Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1

Similar presentations


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

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

2 Syllabus Non-pointer Variables Pointer Variables Levels of Indirection

3 Non-pointer Variables
A standard (non-pointer) variable stores values Declaration: datatype variable_name; Example: int x; x contains a value that is of type datatype The value stored in x is accessed directly & x is memory address where value of x is stored 2

4 What memory operations are performed?
Example: int x, y; x = 3; y = x; What memory operations are performed? 3

5 int x, y; x = 3; y = x; x y x x y x y x y 3 3 3 3 Address Ax
Address Ay copy 3 x Address Ax x Address Ax y Address Ay 3 copy x Address Ax y Address Ay 3 x Address Ax y Address Ay 3 4

6 Pointer Variables A pointer is a variable that can contain only the address of something else That something else may be another variable, function, or data object How are pointers used in C? To allow a function to permanently change arguments To access data or functions indirectly To track dynamically allocated memory at runtime To build advanced data structures such as linked lists 5

7 Declaration: datatype * p_name;
The * symbol declares a variable as a pointer p_name contains an address p_name is said to “reference” or to “point to” the memory location at that address datatype is the type of value stored at the address The * symbol is also used to “dereference” a pointer: If p contains an address, then *p retrieves the value stored at that address (aka indirect access) & p_name is the address in memory where the pointer variable p_name itself is stored 6

8 Example: g is a pointer of type float * and references address 500. The memory at address 500 holds value 1.25., and g itself is stored at address 384: g contains the value 500 * g returns the value 1.25 & g returns the value 384 g 384 500 500 1.25 7

9 A pointer variable should be initialized to a valid address before being used:
Example: float var = 3, count; /* Storage */ float *p1, *p2 = & var; /* Pointers */ p1 = &count; printf( "%p %p %f\n", &p2, p2, *p2 ); NULL is a predefined macro used for initializing a pointer so that it does not point to anything Example: double *p, *q = NULL; p = NULL; 8

10 Examples: int x, y, z; int *p, *q, *r; /* Pointers */ x = 3; p = & x;
q = & y; r = p; y = x; z = *p; *p = 7; *q = *p; 9

11 int *p, *q, *r; /* Pointers */
int x, y, z; int *p, *q, *r; /* Pointers */ x = 3; (store a 3 in x) x Address Ax y Address Ay p Address Ap q Address Aq z Address Az r Address Ar 3 x Address Ax x Address Ax 3 10

12 p = &x; (store address of x in pointer p)
q = &y; (store address of y in pointer q) Ax p Address Ap x Address Ax 3 p Address Ap x Address Ax Ax 3 Ay q Address Aq y Address Ay q Address Aq y Address Ay Ay 11

13 r = p; (copy contents of pointer p to pointer r)
Address Ap r Address Ar Ax r Address Ar Ax p Address Ap x Address Ax 3 12

14 y = x; (copy contents of x to y)
z = *p; (copy contents of x to z via *p) x Address Ax y Address Ay 3 y Address Ay 3 p Address Ap x Address Ax Ax z Address Az 3 z Address Az 3 p contains a memory address. If *p is on the right side of the equal sign, it retrieves the value stored at that memory address. 13

15 *p = 7; (store a 7 in x via *p)
Address Ap x Address Ax Ax 7 3 x Address Ax 7 p contains a memory address. If *p is on the left side of the equal sign, the memory location that p points to will store the value on the right side. 14

16 *q = *p; (copy contents of x to y via pointers)
Address Aq y Address Ay Ay p Address Ap x Address Ax Ax 7 3 y Address Ay 7 p contains a memory address. q contains a memory address. The memory location that q points to will get the value that is pointed to by p. 15

17 Assume addresses: x→100 y→104 z→108 p→120 q→124 r→128
Assume type int is 32 bit, and that pointers are also 32 bit. Start   End x = 3; p = &x; q = &y; r = p; y = x; z = *p; *p = 7; *q = *p; Addr Var Value 100 x 3 7 104 y ? 108 z 120 p 124 q 128 r 16

18 Levels of Indirection C supports multiple levels of indirection; e.g.
Declarations: One level - datatype *pname; Pointer (e.g., int *p;) Two level - datatype **pname; Pointer to a pointer (e.g., float **q;) Three level - datatype ***pname; Pointer to a pointer to a pointer (e.g., double ***w;) 17

19 What does “multiple levels of indirection” mean?
Example: int x; int *p; int **q; Standard variable → It stores a value of type integer. Pointer variable → It holds the address of a location in memory where data is stored. Pointer to a pointer variable → It holds the address of a location in memory that holds the address of another memory location where data is stored. 18

20 Example: int x; int *p; /* Pointer */
int **q; /* Pointer to another pointer */ x = 5; p = &x; /* p points to x */ q = &p; /* q points to p which points to x */ printf("%d %d %d\n", x, *p, **q); /* Displays */ *p = 4; /* One level of indirect access */ printf("%d %d %d\n", x, *p, **q); /* Displays */ **q = 3; /* Two levels of indirect access */ printf("%d %d %d\n", x, *p, **q); /* Displays */ p = NULL; 19

21 Assume addresses: x→1150 p→1300 q→1500
Assume type int is 32 bit, and that pointers are also 32 bit. Start   End x = 5; p = &x; q = &p; *p = 4; **q = 3; p = NULL; Addr Var Value 1150 x 5 4 3 1300 p ? NULL 1500 q Conceptual diagram (just before executing *p = 4;) 1500 1300 q 1300 1150 p 1150 5 x 20

22 With great power comes great responsibility …
Common error → Using a non-initialized pointer Pointer variable p contains random address x = *p; retrieves value from that random address → Program now mistakenly uses incorrect value *p = x; writes a value to that random address → Corrupted memory (may get memory fault message, or no warning at all) Always initialize a pointer to a valid address or NULL. If p is set to NULL, then *p generates a run-time error, which alerts you that a problem exists. 21


Download ppt "ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1"

Similar presentations


Ads by Google