Character Strings No operations for string as a unit A string is just an array of char terminated by the null character \0 The null character makes it easy for programs to detect the end char s[] = "0123456789"; defines s to be an array of 11 chars Functions are passed pointers to the beginning of a string String-copy Example Several implementation of string-copy Copies character string pointed to by t to the area pointed to by s void strcpy(char *s, char *t); int i = 0; while ( (s[i] = t[i])!= \0 ) /* array version */ i++; while ( (*s = *t)!= \0 ) /* pointer version */ {s++; t++;} while ( (*s++ = *t++)!= \0 ) /* short pointer version */ while ( *s++ = *t++) /* shorter pointer version */ 1
Arrays vs. Pointers The difference between: char a[] = "012"; /* array of char */ char *b = "012"; /* pointer to constant array */ b[1] = '5'; /* undefined */ b = a; /* makes b point to a */ String Arguments Equivalent forms of function arguments int strlen( char s[]); int strlen(char *s); Any part of the array can be passed as function argument: char a[] = "0123456789"; strlen(&a[3]); /* pass part of array */ 2
Array of Pointers char *x[10]; /* x is an array of 10 pointers to char */ char *color[] = { red, green, yellow, blue }; Defines color to be an array of pointers to char, each pointing to a constant character string. Color r e d \0 g r e e n \0 y e l l o w \0 b l u e \0 Multi-dimensional Arrays Int magic[3][3] = { {6, 7, 2}, {1, 5, 9}, {8, 3, 4}}; A two dimensional array is an array of arrays magic: [0] [1] [2] magic[0] This explains the notation magic[1] magic[i][j] rather than magic[2] magic[i,j] 3
Passing Multi-dim Arrays to Funs void print(int a[][]) { int i, j; for (i = 0; i < MAXROWS; i++) { for (j = 0; j < MAXCOL; j++) printf("%d ", a[i][j]); printf("\n"); } } Will lead to compiler error! Why? Why is void print(int a[]) fine? Parameter declaration must include number of cols Structures A way to pack different data types as a unit Useful to keep track of mixed data: payroll records student records geometrical shapes data structure nodes 4
Basics of Structures struct tag { member declarations } declared variables; A structure declaration defines a type struct tag { }; /* no storage is allocated */ struct { } x, y, z; int x, y, z; struct tag x, y, z; Examples and Initialization struct point { }; int x; int y; struct point p = { 2, 3}; printf("%d %d\n", p.x, p.y); /* accesses members */ struct triangle { /* a new type: triangle */ }; struct point p1; struct point p2; struct point p3; 5
Structures and Pointers Structures are passed to functions by value This is not efficient for large structures imagine copying thousands of fields How can this be improved? Pass a pointer to a structure still by value but only 4 bytes struct point *pp; declares pp to point an object of type struct point how much storage is allocated? Accessing Members struct point origin; struct point *pp = &origin; Recall that *pp can be used wherever origin is used (*pp).x and (*pp).y Are the brackets needed? Alternative notation pp->x, pp->y are equivalent to (*pp).x, (*pp).y 6
Self-referential Structures Contains a pointer to an object of its own type struct node { int key; struct node *next; }; Why a recursive structure should not be allowed? Typedef typedef: creates new data type from the old typedef int Length; /* Length is now a synonym for int */ typedef struct point { int x; int y; } PointType; PointType p1, p2; 7
Dynamic Memory Allocation Helps create and maintain dynamic data create storage when demanded release when no longer needed Functions malloc, free and the operator sizeof play a key role void *malloc(size_t size); allocates at least size bytes and returns a pointer to the beginning of the memory block returns NULL if something goes wrong free and sizeof void free(void *ptr); releases the block pointed to by ptr the block should have been allocated by malloc or other functions in the family sizeof operator: determines in bytes the size of its operands char a[3]; typedef struct point { int x; char y;} PointType; printf("sizeof char = %d, a = %d, PointType = %d \n", sizeof(char), sizeof a, sizeof(pointtype)); sizeof char = 1, a = 3, PointType = 8 8
Pointer Arithmetic If p points to an object, then p + i points to the i-th object of the same type (i an int) char c[10], *cp; int i[10], *ip; float b[2][20], (*bp)[20]; /* a points to an arrray */ cp = c; ip = i; bp = &b[0]; printf("cp = %p, cp+1 = %p, ip = %p, ip+2 = %p, bp = %p, bp+1 = %p\n", cp, cp+1, ip, ip+2, bp, bp+1); bp++; printf(" bp - &b[0] = %d\n", bp - &b[0]); /* produces and int */ cp = ffbefa6a, cp+1 = ffbefa6b, ip = ffbefa3c, ip+2 = ffbefa44, bp = ffbef998, bp+1 = ffbef9e8 bp - &b[0] = 1 9