Structures, Strings, and Such What about arrays? � Arrays are declared: 0 arr[0] int arr[8]; 0 arr[1] � This declares an array of 8 0 arr[2] integers 0 arr[3] � Memory allocated for it, but 5 0 arr[4] not initialized 0 arr[5] � Arrays are referenced: arr[4] = 5; 0 arr[6] z = arr[4]; 0 arr[7] � No bounds checking: arr[51] 0 won’t signal an error 5 0 z � Not easy to find the length of an array CMPS 12B, UC Santa Cruz Structures, Strings, and Such 2
Arrays and pointers � Arrays are implemented 0 arr[0] with pointers 0 arr[1] � &(arr[0]) == arr == address 0 arr[2] of the first element of the 0 arr[3] array 0 arr[4] � arr[j] == *(arr + j) 0 arr[5] � Any integer can be added to 0 arr[6] a pointer 0 arr[7] � Negative ones, too! 0 � As before, C doesn’t check to see if it looks OK 0 z CMPS 12B, UC Santa Cruz Structures, Strings, and Such 3 Accessing arrays � Common to access arrays in int arr [50]; loops int j; int *p; � Two forms of loops are int sum = 0; common for (j = 0; j < 50; j++) { � Array index changes sum += arr[j]; � Pointer to element changes } (incremented) � Both forms are OK (and for (p = arr; p < (arr+50); p++) { equivalent) sum += *p; } CMPS 12B, UC Santa Cruz Structures, Strings, and Such 4
Strings in C � Strings in C are simply arrays of characters � Strings must be terminated by NUL (\0) � This makes it impossible to include \0 in a string… � Since arrays can’t be resized, nor can strings! � They can be copied onto one another using functions like strcpy() � Strings can be declared like this: � char s1[50]; � char s2[50] = “Hello!”; � Now, s2 is an array of 50 bytes, with the first 7 initialized � char *s3 = “testing”; � Now, s3 points to an array of at least 8 characters (including the trailing \0) � Some smart compilers may share storage for multiple strings initialized to “test”… � Don’t modify strings initialized the third way! CMPS 12B, UC Santa Cruz Structures, Strings, and Such 5 Structures in C A s t r uct ur e is a mechanism for � struc t tre e node { grouping values together int strLe ngth; � Similar to class in Java c har s[8]; � No associated methods! struc t tre e node *l; � May contain builtin types as well struc t tre e node *r; as pointers and other structures }; Refer to elements of a structure � struc t tre e node root; with “.” notation root.strLe ngth = 5; � Different notation for referring // Copy 5 c harac te rs into s to elements of a structure being bc opy (value , root.s, 5); pointed to Useful for building complex data � structures � Pointers may refer to memory used for a structure � How is memory allocated? CMPS 12B, UC Santa Cruz Structures, Strings, and Such 6
Pointers and structures struc t tre e node { � Pointers may point at int strLe ngth; structures c har s[8]; struc t tre e node *l; � p = &root; struc t tre e node *r; � Elements referred to with }; “->” notation struc t tre e node root; � (*p).strLength is equivalent struc t tre e node *p; to p->strLength root.strLe ngth = 5; // Copy 5 c harac te rs into s � How is space allocated? bc opy (value , root.s, 5); � Library call mal l oc p->strLe ngth = 6; bc opy (nValue , p->s, 6); � Compute necessary memory node p = (struc t tre e node *) size using s i zeof function malloc (siz e of (struc t � Works on builtin types, too tre e node ); root.l = node p; � May modify the value CMPS 12B, UC Santa Cruz Structures, Strings, and Such 7 Passing parameters void addOne Wrong (int x) { � Builtin types (int, pointers, x = x+1; } FP) are passed by v al ue void addOne Right (int *x) { � Function cannot change the *x = *x +1; value in the caller } � Functions may change the int main () { value a pointer points at int value = 5; addOne Wrong (value ); � Arrays and structures must printf (“Value is %d\n”, be passed by r ef er ence value ); addOne Right (&value ); � Pass a pointer to the array or printf (“Value is %d\n”, structure value ); � Pointer doesn’t change re turn (0); } � Array or structure may change Value is 5 Value is 6 CMPS 12B, UC Santa Cruz Structures, Strings, and Such 8
Recommend
More recommend