Download presentation
Presentation is loading. Please wait.
Published byΑπόστολος Ἐπαφρᾶς Αποστόλου Modified over 6 years ago
1
Pointers Problem Solving & Program Design in C Eighth Edition
Jeri R. Hanly & Elliot B. Koffman © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
2
Name Addr Content Lecture Lecture++;
3
6.1 Addresses and Pointers Recall memory concepts from Ch2
name address Memory - content 10 11 12 13 14 15 16 … 1 = 7 = ? = arbitrary 1’s and 0’s ???? 8 int x1=1, x2=7; double distance; int *p; int q=8; p = &q; x1 x2 distance 16 p q
4
What is a Pointer? A pointer is a variable that holds the address of a memory location If a variable p holds the address of another variable q, then p is said to point to q If q is a variable at location 16 in memory, then p would have the value 16 (q’s address)
5
How to declare a pointer variable
Pointer variables are declared using an asterisk * before the pointer name int a, b, *ptr; ptr is a pointer to a memory location that can store an integer
6
Address Operator: & A variable can be referenced using the address operator & For the previous example: &x1 is the address of x1 printf(“%d”, &x1); will print 12 (address) printf(“%d”, x1); will print 1 (content)
7
* has different meanings in different contexts
a = x * y; multiplication int *ptr; declare a pointer * is also used as indirection or de-referencing operator in C statements. ptr = &y; a = x * *ptr;
8
Example: Pointers, address, indirection
name address memory 10 11 12 13 14 ? int a, b; int *c, *d; a = 5; c = &a; d = &b; *d = 9; print c, *c, &c print a, b a 5 b 9 c 11 d 12 c=11 *c=5 &c=13 a=5 b=9
9
Exercise: Trace the following code
name address memory 510 511 512 513 514 ? int x, y; int *p1, *p2; x = 3 + 4; y = x / 2 + 5; p1 = &y; p2 = &x; *p1 = x + *p2; *p2 = *p1 + y; print p1, *p1, &p1 print x, &x, y, &y x y p1 p2
10
Declaration, Assignment, Comparison, Type, ARITHMETIC
MORE ABOUT POINTERS Declaration, Assignment, Comparison, Type, ARITHMETIC
11
Declaring pointers When using the form int *p, q;
the * operator does not distribute. In the above example p is declared to be a pointer to int. q is declared to be an int. If you want both to be pointer, then use the form int *p, *q;
12
Assigning values to a pointer
int a; int *iPtr; Variables are not initialized in the above example the assignment operator (=) is defined for pointers As always, the right hand side can be any expression as long as it evaluates to the same type as the left the operator & in front of an ordinary variable produces the address of that variable. a = 4 + 5; iPtr = &a;
13
Exercise int a=1, b=2, *ptr; ptr = &b; a = *ptr; *ptr = 5; a 102 b 104
Give a memory snapshot after each set of assignment statements int a=1, b=2, *ptr; ptr = &b; a = *ptr; *ptr = 5; a 102 b 104 ptr 106
14
NULL pointer A pointer can be assigned or compared to the integer zero, or, equivalently, to the symbolic constant NULL, which is defined in <stdio.h>. A pointer variable whose value is NULL is not pointing to anything that can be accessed
15
Pointer Initialization
iPtr s dPtr int *iPtr=0; char *s=0; double *dPtr=NULL; !!! When we assign a value to a pointer during it is declaration, we mean to put that value into pointer variable (no indirection)!!! int *iPtr=0; is same as int *iPtr; iPtr=0; /* not like *iPtr = 0; */
16
Many-to-One Pointing A pointer can point to only one location at a time, but several pointers can point to the same location. /* Declare and initialize variables. */ int x=-5, y = 8; int *ptr1, *ptr2; /* Assign both pointers to point to x. */ ptr1 = &x; ptr2 = ptr1; The memory snapshot after these statements are executed x 444 -5 y 446 8 Ptr1 448 ptr2 450
17
Exercise Show the memory snapshot after the following operations
int x=2, y=5, temp; int *ptr1, *ptr2, *ptr3; // make ptr1 point to x ptr1 = &x; // make ptr2 point to y ptr2 = &y; 2 5 ? x y temp ptr1 ptr2 ptr3
18
Exercise Show the memory snapshot after the following operations
// swap the contents of // ptr1 and ptr2 ptr3 = ptr1; ptr1 = ptr2; ptr2 = ptr3; 2 5 ? x y temp ptr1 ptr2 ptr3
19
Exercise Show the memory snapshot after the following operations
// swap the values pointed // by ptr1 and ptr2 temp = *ptr1; *ptr1 = *ptr2; *ptr2 = temp; 5 2 5 x y temp ptr1 ptr2 ptr3
20
Pointer types Declaring a pointer creates a variable that is capable of holding an address And addresses are integers! But, the type we specify in the declaration of a pointer is the type of variable to which this pointer points !!! a pointer defined to point to an integer variable cannot also point to a float/double variable even though both holds integer address values !!!
21
Example: pointers with different types
5 23.453 102 106 int a=5; double b=23.452; int *iPtr; double *dPtr; iPtr = &a; dPtr = &b; the variable iPtr is declared to point to an int the variable dPtr is declared to point to a double a 102 b 106 iPtr 114 dPtr 118 122
22
Comparing Pointers You may compare pointers using >,<,== etc.
Common comparisons are: check for null pointer if (p == NULL) … check if two pointers are pointing to the same location if (p == q) … Is this equivalent to if (*p == *q) … Then what is if (*p == *q) … compare two values pointed by p and q
23
Name Addr Content Lecture Lecture++;
24
6.4 Pointers in Function References (!IMPORTANT!)
In C, function references are call-by-value except when an array name is used as an argument. An array name is the address of the first element Values in an array can be modified by statements within a function To modify a function argument, a pointer to the argument must be passed scanf(“%f”, &X); This statement specifies that the value read is to be stored at the address of X The actual parameter that corresponds to a pointer argument must be an address or pointer.
25
Example: Pass by Reference
Modify behaviour in argument passing void f(int j) { j = 5; } void g() int i = 3; f(i); void f(int *ptr) { *ptr = 5; } void g() int i = 3; f(&i); The program segment on the left is demonstrating “pass by value”, i.e. the value, actually a copy, of the variable i is passed as argument to function f(). The other program shows you “pass by reference”. The address of the variable i is passed to f() instead, thus allowing us to modify the content stored in the variable. i = 3 i = ? i = ? i = 5
26
An Illustration Data Table Name Type Description Value i int
int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j 10
27
An Illustration Data Table Name Type Description Value i int
int i = 5, j = 10; int *ptr; /* declare a pointer-to-integer variable */ int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j 10 ptr int * integer pointer variable
28
An Illustration Data Table Name Type Description Value i int
int i = 5, j = 10; int *ptr; int **pptr; /* declare a pointer-to-pointer-to-integer variable */ ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j 10 ptr int * integer pointer variable pptr int ** integer pointer pointer variable Double Indirection
29
An Illustration Data Table Name Type Description Value i int
int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; /* store address-of i to ptr */ pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable *ptr de-reference of ptr
30
An Illustration Data Table Name Type Description Value i int
int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 3 j 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *ptr de-reference of ptr
31
An Illustration Data Table Name Type Description Value i int
int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr **pptr de-reference of de-reference of pptr
32
An Illustration Data Table Name Type Description Value i int
int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j 10 ptr int * integer pointer variable address of j pptr int ** integer pointer pointer variable address of ptr *ptr de-reference of ptr
33
An Illustration Data Table Name Type Description Value i int
int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j 9 ptr int * integer pointer variable address of j pptr int ** integer pointer pointer variable address of ptr **pptr de-reference of de-reference of pptr
34
An Illustration Data Table Name Type Description Value i int
int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j 9 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *pptr de-reference of pptr value of ptr (address of i)
35
An Illustration Data Table Name Type Description Value i int
int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable -2 j 9 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *ptr de-reference of ptr
36
Call by Value swap(x,y); void swap(int a,int b) { int temp; temp = a;
a = b; b = temp; return; } main() { int x = 2, y = 3; printf("%d %d\n“,x,y); swap(x,y); } Changes made in function swap are lost when the function execution is over
37
Call by reference swap2(&x, &y); void swap2(int *aptr, main()
int *bptr) { int temp; temp = *aptr; *aptr = *bptr; *bptr = temp; return; } main() { int x = 2, y = 3; printf("%d %d\n“,x,y); swap2(&x, &y); } Changes made in function swap are done on original x and y and. So they do not get lost when the function execution is over
38
Trace a program main() { int x0=5, x1=2, x2=3; int *p1=&x1, *p2;
swap2(&x0, &x1); swap2(p1, p2); printf(“%d %d %d\n”, x0, x1, x2); } void swap2(int *a, int *b) int tmp; tmp = *a; *a = *b; *b = tmp; return; Name Addr Value x0 1 x1 2 x2 3 p1 4 p2 5 6 a 7 b 8 tmp 9
39
Now we can get more than one value from a function
Write a function to compute the roots of quadratic equation ax^2+bx+c=0. How to return two roots? void comproots(int a,int b,int c, double *dptr1, double *dptr2) { *dptr1 = (-b - sqrt(b*b-4*a*c))/(2.0*a); *dptr2 = (-b + sqrt(b*b-4*a*c))/(2.0*a); return; }
40
Exercise cont’d main() { int a,b,c; double root1,root2;
printf("Enter Coefficients:\n"); scanf("%d %d %d",&a,&b,&c); computeroots(a,b,c,&root1,&root2); printf("First Root = %lf\n",root1); printf("Second Root = %lf\n",root2); }
41
Trace a program main() { int x, y; max_min(4, 3, 5, &x, &y);
printf(“ First: %d %d”, x, y); max_min(x, y, 2, &x, &y); printf(“Second: %d %d”, x, y); } void max_min(int a, int b, int c, int *max, int *min) *max = a; *min = a; if (b > *max) *max = b; if (c > *max) *max = c; if (b < *min) *min = b; if (c < *min) *min = c; printf(“F: %d %d\n”, max, *max); name Addr Value x 1 y 2 3 4 5 a 6 b 7 c 8 max 9 min 10
42
Name Addr Content Lecture Lecture++;
43
Pointer types Declaring a pointer creates a variable that is capable of holding an address And addresses are integers! But, the type we specify in the declaration of a pointer is the type of variable to which this pointer points !!! a pointer defined to point to an integer variable cannot also point to a float/double variable even though both holds integer address values !!!
44
Example: pointers with different types
5 23.453 102 106 int a=5; double b=23.452; int *iPtr; double *dPtr; iPtr = &a; dPtr = &b; the variable iPtr is declared to point to an int the variable dPtr is declared to point to a double a 102 b 106 iPtr 114 dPtr 118 126
45
Pointer Arithmetic Four arithmetic operations are supported
+, -, ++, -- only integers may be used in these operations Arithmetic is performed relative to the variable type being pointed to MOSTLY USED WITH ARRAYS (see next section) Example: p++; if p is defined as int *p, p will be incremented by 4 (system dependent) if p is defined as double *p, p will be incremented by 8(system dependent when applied to pointers, ++ means increment pointer to point to next value in memory
46
Pointer Arithmetic What’s ptr + 1? The next memory location!
The previous memory location! What’s ptr * 2 and ptr / 2? Invalid operations!!!
47
More Pointer Arithmetic
What if a is a int array? Given int *ptr = a; What’s ptr + 1 then? Addr Content 1000 a[0]: 37.9 1001 … 1002 1003 1004 a[1]: 1.23 1005 1006 1007 1008 a[2]: 3.14 1009 1010 1011
48
More Pointer Arithmetic
Given double *ptr = a; What’s ptr + 1 then? According to the type of the pointer: sizeof(double) = = 1008 Addr Content 1000 a[0]: 37.9 1001 … 1002 1003 1004 ….. 1005 1006 1007 1008 a[1]: 3.14 1009 1010 1011
49
Advice and Precaution Pros Cons Efficiency Convenience Error-prone
Difficult to debug
50
6.2 Pointers and Arrays The name of an array is the address of the first elements (i.e. a pointer to the first element) The array name is a constant that always points to the first element of the array and its value can not be changed. Array names and pointers may often be used interchangeably. Example int num[4] = {1,2,3,4}, *p, q[]; p = num; q = p; // or q = num; /* above assignment is the same as p = &num[0]; */ printf(“%i”, *p); // print num[0] p++; printf(“%i”, *p); // print num[1] printf(“%i”, *q); // print num[0] printf(“%i”, *(p+2)); // print num[2]
51
Pointer Arithmetic and Array
float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] a[2] a[3] ptr float * float pointer variable address of a[2] *ptr de-reference of float pointer variable
52
Pointer Arithmetic and Array
float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] a[2] 3.14 a[3] ptr float * float pointer variable address of a[2] *ptr de-reference of float pointer variable
53
Pointer Arithmetic and Array
float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] a[2] 3.14 a[3] ptr float * float pointer variable address of a[3] *ptr de-reference of float pointer variable
54
Pointer Arithmetic and Array
float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] a[2] 3.14 a[3] 9.0 ptr float * float pointer variable address of a[3] *ptr de-reference of float pointer variable
55
Pointer Arithmetic and Array
float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] a[2] 3.14 a[3] 9.0 ptr float * float pointer variable address of a[0] *ptr de-reference of float pointer variable
56
Pointer Arithmetic and Array
float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] ? a[2] 3.14 a[3] 9.0 ptr float * float pointer variable address of a[0] *ptr de-reference of float pointer variable
57
Pointer Arithmetic and Array
float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] ? a[2] 3.14 a[3] 9.0 ptr float * float pointer variable address of a[2] *ptr de-reference of float pointer variable
58
Pointer Arithmetic and Array
float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] ? a[2] 7.0 a[3] 9.0 ptr float * float pointer variable address of a[2] *ptr de-reference of float pointer variable
59
Two Dimensional Arrays
A two-dimensional array is stored in sequential memory locations, in row order. int s[2][3] = {{2,4,6}, {1,5,3}}; int *sptr = &s[0][0]; Memory allocation: s[0][0] 2 s[0][1] 4 s[0][2] 6 s[1][0] 1 s[1][1] 5 s[1][2] 3 A pointer reference to s[0][1] would be *(sptr+1) A pointer reference to s[1][1] would be *(sptr+4) row offset * number of columns + column offset
60
Pointers to struct /* define a struct for related student data */
typedef struct student { char name[50]; char major [20]; double gpa; } STUDENT; STUDENT bob = {"Bob Smith", "Math", 3.77}; STUDENT sally = {"Sally", "CSEE", 4.0}; STUDENT *pStudent; /* pStudent is a "pointer to struct student" */ /* make pStudent point to bob */ pStudent = &bob; /* use -> to access the members */ printf ("Bob's name: %s\n", pStudent->name); printf ("Bob's gpa : %f\n", pStudent->gpa); /* make pStudent point to sally */ pStudent = &sally; printf ("Sally's name: %s\n", pStudent->name); printf ("Sally's gpa: %f\n", pStudent->gpa); Note too that the following are equivalent. Why?? pStudent->gpa and (*pStudent).gpa /* the parentheses are necessary */
61
Pointer to struct for functions
void PrintStudent(STUDENT *studentp) { printf(“Name : %s\n”, studentp->name); printf(“Major: %s\n”, studentp->major); printf(“GPA : %4.2f”, studentp->gpa); }
62
6.7 Dynamic Memory Allocation
Dynamically allocated memory is determined at runtime A program may create as many or as few variables as required, offering greater flexibility Dynamic allocation is often used to support data structures such as stacks, queues, linked lists and binary trees. Dynamic memory is finite Dynamically allocated memory may be freed during execution
63
Dynamic Memory Allocation
Memory is allocated using the: malloc function (memory allocation) calloc function (cleared memory allocation) Memory is released using the: free function The size of memory requested by malloc or calloc can be changed using the: realloc function
64
malloc and calloc Both functions return a pointer to the newly allocated memory If memory can not be allocated, the value returned will be a NULL value The pointer returned by these functions is declared to be a void pointer A cast operator should be used with the returned pointer value to coerce it to the proper pointer type
65
Example of malloc and calloc
int n = 6, m = 4; double *x; int *p; /* Allocate memory for 6 doubles. */ x = (double *)malloc(n*sizeof(double)); /* Allocate memory for 4 integers. */ p = (int *)calloc(m,sizeof(int)); X p
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.