2. Strings are one-dimesional arrays of char and
terminated by the end-of-string sentinel 0 or null
character .
String constant are written between double quotes.
For example "abc" is a character array of size 4, with
the last element being the null character 0.
char *p ="abc"
Printf ("%s%sn",p,p+1);
2
Strings
3. char *p ="abcde";
char s[]={'a', 'b', 'c', 'd', 'e', '0'};
#include <stdio.h>
#include <stdlib.h>
void f(int a[]);
int main(void) {
char s[] = "Pointer is heart of the C programming";
char *p = " Pointer is heart of the C programming";
int a[3];
double d[5];
printf("%s%dn%s%dn%s%dn%s%dn", "sizeof(s) = ", sizeof(s),
"sizeof(p) = ", sizeof(p), "sizeof(a) = ", sizeof(a),
"sizeof(d) = ", sizeof(d));
f(a);
return EXIT_SUCCESS;
}
void f(int a[]) {
printf("In f(): sizeof(a) = %dn", sizeof(a));
} 3
Strings
4. #include <stdio.h>
#include <stdlib.h>
int find_size(char *);
int main(void) {
char *s = "Pointer is heart of the C programming";
printf("Character of String : %d", find_size(s));
return EXIT_SUCCESS;
}
int find_size(char *s) {
int sum = 0;
while (*s != '0') {
while(isspace(*s)) s++;
if (*s!='0'){
sum++;
s++;
}
}
return sum;
} 4
Characters of Strings
5. #include <stdio.h>
#include <stdlib.h>
int find_size(char *);
int main(void) {
char *s = "Pointer is heart of the C programming ";
printf("Words of String : %d", find_size(s));
return EXIT_SUCCESS;
}
int find_size(char *s) {
int sum = 0;
while (*s != '0') {
while(isspace(*s)) s++;
if (*s!='0'){
sum++;
while (!isspace(*s) && *s!='0') s++;
}
}
return sum;
}
5
Words of Strings
6. #include <stdio.h>
#include <stdlib.h>
void *str_cpy(char *, register const
char *);
int str_len(const char *);
int main(void) {
char c[] = "Pointers is important for C
programmer";
char *s1 = malloc((str_len(c) + 1) *
sizeof(char));
printf("%s", str_cpy(s1, c));
return EXIT_SUCCESS;
}
6
String copy Function
int str_len(const char *s1) {
int length = 0;
while (*s1++ != '0')
length++;
return length;
}
void *str_cpy(char *s1, register
const char *s2) {
register char *p = s1;
while (*p++ = *s2++)
;
return s1;
}
7. int a[3][5];
Starting at the base adress of the array, all the array
elements are stored contiguously in memory. The array
name a by itself is equivalent to &a[0]. It is pointer to an
array of 5 ints.
7
Multidimesional Array
8. In C, array elements can be of any type, including
pointer type.
8
Array of pointers
9. #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXWORD 50
#define N 10
void wrt_words(char **);
void swap(char **, char **);
void sort_words(char **);
int main(void) {
char word[MAXWORD];
char *w[N];
int n, i;
for (i = 0; i < N; i++) {
scanf("%s", word);
fflush(stdout);
w[i] = calloc(strlen(word) + 1,
sizeof(char));
strcpy(w[i], word);
}
sort_words(w);
wrt_words(w);
return EXIT_SUCCESS;
}
9
Example
void sort_words(char **a) {
int i, j;
for (i = 0; i < N; i++) {
for (j = i + 1; j < N; j++) {
if
(strcmp(a[i], a[j]) > 0)
swap(&a[i], &a[j]);
}
}}
void swap(char **p, char **q) {
char *temp;
temp = *p;
*p = *q;
*q = temp;
}
void wrt_words(char **a) {
int i;
for (i = 0; i < N; i++) {
printf("%sn", a[i]);
}
}
10. typedef is a keyword used in C language to assign alternative
names to existing types. Its mostly used with user defined
data types, when names of data types get slightly
complicated. Also typedef improves portability of your
product. Following is the general syntax for using typedef,
typedef existing_name alias_name
typedef unsigned long ulong;
ulong i, j ;
10
typedef
11. Enumerated Types are a special way of creating your own
Type in C
enum boolean {true,false};
enum day
{Pazar,Pazartesi,Sali,carsamba,persembe,Cuma,Cumartesi}
enum day =Pazar;
11
Enumerated Types
12. The structure mechanism provides a means to aggregate
variables of different types. Let us define a structure that
describes student that has name, surname and number.
struct student{
int sequence;
char *name;
char *surname;
char *number;
};
struct student s1,s2; 12
Structure and Unions
14. A union, like a structure, is a derived type. Unions follow the same
syntax as structures but have members that share storage. A union
type defines a set of alternative values that may be stored in a
shared portion of memory. The programmer is responsible for
interpreting the stored values correctly. Consider the declaration
typedef union int_or_float {
int i;
float f;
} number;
14
Unions
15. #include <stdio.h>
#include <stdlib.h>
typedef union int_or_float {
int i;
float f;
} number;
int main(void) {
number n;
n.i = 10;
printf("d: %dn", n.i);
n.f = 20.0;
printf("f: %fn", n.f);
printf("d: %dn", n.i);
return EXIT_SUCCESS;
}
15
Example