1 / 22

CSA2090: Systems Programming Introduction to C

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.

keran
Télécharger la présentation

CSA2090: Systems Programming Introduction to C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Aims and Objectives • More about pointers • Traversing arrays using pointers • Pointer arithmetic • Strings 2 of 22 cstaff@cs.um.edu.mt

  3. 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

  4. 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

  5. Pointer sizes 5 of 22 cstaff@cs.um.edu.mt

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. Pointers and Strings • What will happen? • strcpy(str3, “horror”) 14 of 22 cstaff@cs.um.edu.mt

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

More Related