Presentation is loading. Please wait.

Presentation is loading. Please wait.

Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.

Similar presentations


Presentation on theme: "Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the."— Presentation transcript:

1 Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the programs is up, but if the results don't get back today, the due date for the corrections will change. 1

2 declaring strings Suppose you can assume a string won’t be longer than 64 characters. How long do you need to make the character array? 2

3 declaring strings Suppose you can assume a string won’t be longer than 64 characters. How long do you need to make the character array? 65 characters! Why? Because all strings must end with a NULL (‘\0’). If the string has 64 letters, the character array must have 65 letters to have a place for the ‘\0’ 3

4 Reminder C does not initialize variable values. If a value (including array values) are not set by the programmer, they can have any value. 4

5 Example 1 5 str_rev is a function of type “char *”, i.e., returns a pointer to a character the argument is an array (its size is not part of its type) string library functions array  pointer

6 Example 1… 6 figure out where the ‘\0’ is use this to control how many array elements to processes

7 Example 1… 7

8 Example 2: string reversal using pointers 8 array  pointer

9 Example 2… 9 abcd\0 s tptr

10 Example 2… 10

11 When 1 = 4 11 pointers of different types pointer arithmetic: add 1 to pointers of different types

12 When 1 = 4… 12 -o : “put the output in the file specified, instead of the default a.out” but each pointer was incremented by 1!!!

13 What’s going on Pointer arithmetic is performed relative to the size of the pointee type – for char* pointers, “+= 1” increments by 1 – for int* pointers, “+= 1” increments by 4 (if size of int = 4)  in general, “+= 1” will increment a pointer by the size (in bytes) of the type being pointed at analogously for other arithmetic Reason: portability: want code to be able to step through an array of values without worrying about architecture-dependent issues of their size 13

14 Figuring out sizes: sizeof() 14 sizeof() invoked with a type name sizeof() invoked with a variable name sizeof() invoked with a pointer dereference

15 More sizeof() sizeof() applied to an array returns the total size of that array – but be careful about implicit array/pointer conversions 15 what is passed to f() is a pointer, not the whole array

16 Dereferencing+updating pointers A common C idiom is to use an expression that  gives the value of what a pointer is pointing at; and  updates the pointer to point to the next element : *p++ – similarly: *p--, *++p, etc. 16 parsed as: * ( p++ ) evaluates to: value of p = some address a (side effect: p incremented by ‘++’) evaluates to: contents of location a = *p (side effect: p incremented by ‘++’)

17 Walking a pointer down an array 17 dereference the pointer to access memory, then increment the pointer

18 *p++ vs. (*p)++ after x = (*p)++ 18 p x p x after x = *p++ p x

19 Two common pointer problems Uninitialized pointers – the pointer has not been initialized to point to a valid location Dangling pointers – the pointer points at a memory location that has actually been deallocated 19

20 Background: Runtime Memory Organization Layout of an executing process’s virtual memory: 20 code global data memory mapped files operating system stack (grows downwards) heap (grows upwards) high addresses low addresses 0xffffffff 0x00000000


Download ppt "Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the."

Similar presentations


Ads by Google