220 likes | 379 Views
CSA2090: Systems Programming Introduction to C. Lecture 5: Pointers and Strings. Dr. Christopher Staff Department of Computer Science & AI University of Malta. Aims and Objectives. More about pointers Traversing arrays using pointers Pointer arithmetic Strings. Pointer types.
E N D
CSA2090:Systems ProgrammingIntroduction to C Lecture 5: Pointers and Strings Dr. Christopher Staff Department of Computer Science & AI University of Malta 1 of 22 cstaff@cs.um.edu.mt
Aims and Objectives • More about pointers • Traversing arrays using pointers • Pointer arithmetic • Strings 2 of 22 cstaff@cs.um.edu.mt
Pointer types • Pointers are not just memory addresses • They are also variables, so they have types • int *p, p is a pointer-to-int • char *c, c is a pointer-to-char • struct person *fred, fred is a pointer-to-struct-person 3 of 22 cstaff@cs.um.edu.mt
Pointer sizes • Pointers are always 4 bytes: • depending on the system architecture • they must be large enough to address the largest supported memory address • But the size of the region that they point to depends on the size of the type of the data they point to 4 of 22 cstaff@cs.um.edu.mt
Pointer sizes 5 of 22 cstaff@cs.um.edu.mt
Pointer sizes • If fred is struct person *, how many bytes in memory does fred point to? struct person { int age; int height; char surname[20]; }*fred; 6 of 22 cstaff@cs.um.edu.mt
Pointer arithmetic • So C knows how many bytes are occupied by the data pointed at by a pointer • Useful especially when arrays are traversed using pointers • More later… 7 of 22 cstaff@cs.um.edu.mt
Pointers and Arrays ptr = ptr + 1 • If ptr is a pointer-to-char then ptr will increase by just one byte • But if ptr is a pointer-to-int, then ptr will increase by 4 bytes 8 of 22 cstaff@cs.um.edu.mt
Pointers and Arrays int numbers[10], i; int *iptr = &numbers[0]; numbers[0]=1; numbers[1]=2; numbers[2]=3; for (i=0; i<3; i++) { printf(“*iptr is %d\n”, *iptr); iptr=iptr+1; } 9 of 22 cstaff@cs.um.edu.mt
Pointers and Structures • struct person fred; • fred.age • struct person *fred; • (*fred).age • (*fred).age becomes easy to make mistakes with, especially when member is a pointer • fred->age 10 of 22 cstaff@cs.um.edu.mt
Pointers and Functions • Let’s say that we want to create a variable in a calling function, and pass it to a called function so that we can store some result in it… • See func.c, func_ptr1.c, func_ptr2.c 11 of 22 cstaff@cs.um.edu.mt
Strings • In C a string is just an array of characters, with a NULL or zero byte terminating the string • String handling functions in string.h expect to find trailing \0 12 of 22 cstaff@cs.um.edu.mt
Strings • char str1[10]; • char str2[] = “hello”; // \0 added by C • char *str3; // uninitialised • str1 = “goodbye”; // illegal!!! • strcpy(str1, “goodbye”); • But C doesn’t do array bounds checking! 13 of 22 cstaff@cs.um.edu.mt
Pointers and Strings • What will happen? • strcpy(str3, “horror”) 14 of 22 cstaff@cs.um.edu.mt
Pointers and Strings • What will happen? • strcpy(str3, “horror”); • Because str3 was not initialised, it will contain “garbage” data… • … which C will attempt to interpret as an address… • … and try to store string at that location 15 of 22 cstaff@cs.um.edu.mt
Pointers and Strings • This can result in… • Trying to access protected memory: segmentation fault • Trying to access unaddressable memory, e.g. into the middle of a byte or byte 0: bus error • Appearing to be successful, but then another process legally overwrites the data 16 of 22 cstaff@cs.um.edu.mt
Initialising pointers • Always initialise a pointer • If no initial value, then use NULL • char *cptr = NULL; • And then always test for NULL pointer before retrieving values via the pointer! if (!cptr) { printf(“Error occurred”); } 17 of 22 cstaff@cs.um.edu.mt
Pointer Arithmetic char c = “hello”; char *cptr = &c; • These statements are equivalent… • c = cptr = &c[0] • c[3] = cptr[3] = *(cptr+3) = *(c+3) • C treats c as a pointer to an array! • You cannot pass by value an array to a function • You cannot return an array from a function! 18 of 22 cstaff@cs.um.edu.mt
Passing arrays to functions char c = “hello”; printstring(c); … void printstring(char c[]){…} \\ OR void printstring(char *c){…} • Some caveats for using char *: see Love, 10 19 of 22 cstaff@cs.um.edu.mt
Pointers-to-pointers-to… • A character string is really a pointer to an array of characters • But what if we want a two dimensional array? • an array of strings, for instance • char c[][] OR char **c 20 of 22 cstaff@cs.um.edu.mt
Differences • Can initialise char c[][] = {“dog”, “cat”, “horse”, “bat”} • Cannot initialise char **c in the same way! • Why? 21 of 22 cstaff@cs.um.edu.mt
Next week • Lab sessions for exercise 2 in Love • Attempt Exercises 11.1 to 11.5 at home • First part of lab session will cover any problems with these, and then attempt 11.6 to 11.8 in lab • After Lab session, read Love, 12 on your own 22 of 22 cstaff@cs.um.edu.mt