Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2

Similar presentations


Presentation on theme: "ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2"— Presentation transcript:

1 ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Herbert G. Mayer, PSU CS Status 8/4/2015 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE

2 Syllabus Array Variables Pointer Variables and 1-D Arrays
Pointer Arithmetic Pointers and Strings Examples

3 Array Variables Declaration: datatype var_name[SIZE];
Example: int x[3]; x is the address of the first element in the array. x is an array. It is not a pointer. The address value cannot be changed. Example: x = 3278; /* Error! */ x[k], where k is a non-negative integer index, is the value stored at index k in the array. 2

4 x[0] x[1] x[2] x[0] x[1] x[2] x[0] x[1] x[2]
int x[3]; x[0] = 3; x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x 3 x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x 3 3

5 Pointer Variables and 1-D Arrays
Declaration: datatype * pointer_name; Example: int *p; p contains the address of a location in memory. The address stored in p can be changed. & p is the memory address where the pointer variable itself is stored. 4

6 If a pointer p contains the address of an array, then either pointer notation or array notation can be used to access the array. Pointer notation Uses the * symbol, pointer name, integer offset: *(p+k) where k is an integer (neg/zero/pos) Array notation Uses pointer name, brackets, integer index: p[k] where k is an integer (neg/zero/pos) 5

7 Example: int *p; /* Pointer to data array */
int x[3]; /* Data array */ p = x; /* p now points to the first element of array x */ p = &x[0]; /* This does the same thing */ x[0] = 3; /* Store a 3 in the first element of array x */ *(p+0) = 3; /* This does the same thing */ p[0] = 3; /* This does the same thing */ x[1] = 5; /* Store a 5 in the second element of array x */ *(p+1) = 5; /* This does the same thing */ p[1] = 5; /* This does the same thing */ p = &x[2]; /* p now points to the third element of array x */ x[0] = 7; /* Store a 7 in the first element of array x */ *(p+0) = 9; /* Store a 9 in the third element of array x */ 6

8 x[0] x[1] x[2] p x[0] x[1] x[2] p
int *p; int x[3]; p = x; p = &x[0]; x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap A 7

9 x[0] x[1] x[2] p x[0] x[1] x[2] p
*(p+0) = 3; /* Same as *p = 3; */ p[0] = 3; x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap 3 A x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap A 3 8

10 x[0] x[1] x[2] p x[0] x[1] x[2] p
*(p+1) = 5; /* p+1 is a 4 byte offset */ p[1] = 5; x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap 5 A A+4 3 x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap A 3 5 9

11 p = &x[2]; x[0] x[1] p x[2] Array x 3 5 Address A Address A+4
Address Ap A+8 3 5 10

12 x[0] x[1] x[2] p x[0] x[1] x[2] p
Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap 9 A+8 7 3 5 x[0] Address A x[1] Address A+4 x[2] Address A+8 Array x p Address Ap A+8 7 5 9 11

13 Pointer Arithmetic The address value stored in a pointer can be incremented or decremented. Final address depends on the declared datatype. Integer offsets can be added to or subtracted from a pointer variable. Example: p = p + 1; /* Increment address by one offset */ p = p + 3; /* Increment address by three offsets */ p = p - 1; /* Decrement address by one offset */ p = p - 3; /* Decrement address by three offsets */ 12

14 The increment/decrement operators advance a pointer by one offset (++) or back up the pointer by one offset (--). Example: ++p; /* Increment address by one offset (pre) */ p++; /* Increment address by one offset (post)*/ --p; /* Decrement address by one offset (pre) */ p--; /* Decrement address by one offset (post)*/ Two pointers can be compared. What is actually being compared are the memory addresses. It is not legal to add or multiply two pointers together, or to divide one pointer by another. 13

15 Combining Dereferencing and ++/--
* and ++/-- are often combined in a single expression. The rules of precedence and associativity provide guidance. Precedence Description Operator Associativity 1 (Highest) Increment (post) expr++ left-to-right Decrement (post) expr-- 2 Indirection * right-to-left Reference (addr) & Increment (pre) ++expr Decrement (pre) --expr 14

16 *p++ or *(p++) and *p-- or *(p--)
Current address A in p is retrieved. Address in p is incremented or decremented. Value stored at address A is returned. Example: (Assume 32-bit integers) int x = 100; int y; int *p = &x; x is stored at address 2000. State p x y Initial 2000 100 ? After y=*p++; 2004 15

17 Example: (Assume 32-bit integers) int x = 100; int y; int *p = &x;
(*p)++ and (*p)-- Current address A in p is retrieved. Value stored at address A is returned. Value at address A is incremented or decremented. Example: (Assume 32-bit integers) int x = 100; int y; int *p = &x; x is stored at address 2000. State p x y Initial 2000 100 ? After y=(*p)++; 101 16

18 Example: (Assume 32-bit integers) int x = 100; int y; int *p = &x;
++*p and --*p Current address A in p is retrieved. Value at address A is incremented or decremented. Value stored at address A is returned. Example: (Assume 32-bit integers) int x = 100; int y; int *p = &x; x is stored at address 2000. State p x y Initial 2000 100 ? After y=++*p; 101 17

19 Example: (Assume 32-bit integers) int x = 100; int y; int *p = &x;
*++p and *--p Address in p is incremented or decremented. Current address A in p is retrieved. Value stored at address A is returned. Example: (Assume 32-bit integers) int x = 100; int y; int *p = &x; x is stored at address 2000. State p x y Initial 2000 100 ? After y=*++p; 2004 18

20 Pointers and Strings An array of type char is used to a hold string: char s[10]; A pointer of type char is used to point to a string: char *p; Declaring a pointer to a string does not cause memory to be reserved for holding the string. The correct way to allocate memory for the string is to declare an array of characters. 19

21 A pointer can serve as an alias for the actual string. Example:
char s1[10] = "ECE103", s2[10]; /* Arrays to hold the strings */ char *src, *dst; /* Pointers to source and destination strings */ strcpy(&s2[0],&s1[0]); /* Works fine */ strcpy(s2, s1); /* Works fine */ strcpy(dst, src); /* Valid syntax, but fails since uninitialized */ /* pointers to char are not real strings */ src = s1; dst = s2; /* Initialize pointers to the actual arrays */ strcpy(dst, src); /* Now this will work */ strcpy(dst, s1); /* OK */ strcpy(s2, src); /* OK */ 20

22 Example: 21 #include <stdio.h> #include <string.h>
int main (void) { char s[] = "Hello\n"; /* Store a string in the character array */ char *p = s; /* p is a work pointer */ int k, len = strlen(s); for (k = 0; k < len; k++) /* Display the string using array notation */ printf("%c", p[k]); for (k = 0; k < len; k++) /* Display the string using pointer offsets */ printf("%c", *(p+k)); while (*p != '\0') /* Display the string by incrementing the pointer itself */ printf("%c", *p); p++; } p = s; /* Need to make p point to the start of s again */ while (*p) /* More compact way to display the string */ printf("%c", *p++); return 0; 21

23 Example: #include <stdio.h> #include <string.h>
int main (void) { char s[] = "ECE103"; /* Store a string in the character array */ char *p; /* p is a work pointer */ p = s + strlen(s) - 1; /* p points to the end of the string */ /* Display the string in reverse */ while (p >= s) printf("%c", *p); p--; } printf("\n"); return 0; 22


Download ppt "ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2"

Similar presentations


Ads by Google