Download presentation
Presentation is loading. Please wait.
Published byClementine Casey Modified over 8 years ago
1
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 1 of 22 cstaff@cs.um.edu.mt CSA2090: Systems Programming Introduction to C Dr. Christopher Staff Department of Computer Science & AI University of Malta Lecture 5: Pointers and Strings
2
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 2 of 22 cstaff@cs.um.edu.mt Aims and Objectives More about pointers Traversing arrays using pointers Pointer arithmetic Strings
3
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 3 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
4
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 4 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
5
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 5 of 22 cstaff@cs.um.edu.mt Pointer sizes
6
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 6 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;
7
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 7 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…
8
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 8 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
9
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 9 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; }
10
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 10 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
11
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 11 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
12
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 12 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
13
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 13 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!
14
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 14 of 22 cstaff@cs.um.edu.mt Pointers and Strings What will happen? –strcpy(str3, “horror”)
15
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 15 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
16
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 16 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
17
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 17 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”); }
18
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 18 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!
19
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 19 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
20
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 20 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
21
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 21 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?
22
University of Malta CSA2090: Lecture 5 © 2004- Chris Staff 22 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.