380 likes | 503 Vues
This guide provides a comprehensive overview of pointers in C programming. We explore how pointers are variables that hold addresses of other variables, using the unary operator `&` for referencing and `*` for dereferencing. Key examples illustrate assigning and accessing values through pointers, demonstrating both pointers to single integers and arrays. With step-by-step explanations and code snippets, readers will learn how to effectively use pointers, understand their importance in memory management, and avoid common pitfalls in pointer operations.
E N D
C … 7 3 4 … 172 173 174 175 176 177 178 179 180 181 P … 174 3 4 … 832 833 834 835 836 837 838 839 840 841 Pointers • Pointer is a variable that contains the address of a variable • Here P is sahd to point to the variable C
Referencing • The unary operator & gives the address of a variable • The statement P=&C assigns the address of C to the variable P, and now P points to C • To print a pointer, use %p format.
Referencing int C; int *P; /* Declare P as a pointer to int */ C = 7; P = &C; C … 7 3 4 … 172 173 174 175 176 177 178 179 180 181 P … 174 3 4 … 832 833 834 835 836 837 838 839 840 841
Dereferencing • The unary operator * is the dereferencing operator • Applied on pointers • Access the object the pointer points to • The statement *P=5; Puts in C (the variable pointed by P) the value 5
Dereferencing printf(“%d”, *P); /* Prints out ‘7’ */ *P = 177; printf(“%d”, C); /* Prints out ‘177’ */ P = 177; /* This is unadvisable! */ C … 7 3 4 … 177 172 173 174 175 176 177 178 179 180 181 P … 177 174 3 4 … 832 833 834 835 836 837 838 839 840 841
Example pointers.c
pointers.c – step by step x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 2 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 … 564 772
pointers.c – step by step x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 2 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 120 564 772
pointers.c – step by step x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 2 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 120 564 772
pointers.c – step by step x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 1 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 120 564 772
pointers.c – step by step x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 1 1 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 120 564 772
pointers.c – step by step x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0; /* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 0 1 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 120 564 772
pointers.c – step by step x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0;/* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 0 1 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 120 564 772
pointers.c – step by step x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0;/* x is now 0 */ printf("x is now %d\n",x); ip = &z[2]; /* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 0 1 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 372 564 772
pointers.c – step by step x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0;/* x is now 0 */ printf("x is now %d\n",x); ip = &z[2];/* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 0 1 120 248 Z[0] Z[1] Z[2] 5 6 7 364 368 372 z ip 364 372 564 772
pointers.c – step by step x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0;/* x is now 0 */ printf("x is now %d\n",x); ip = &z[2];/* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 0 1 120 248 Z[0] Z[1] Z[2] 5 6 1 364 368 372 z ip 364 372 564 772
pointers.c – step by step x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0;/* x is now 0 */ printf("x is now %d\n",x); ip = &z[2];/* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 0 1 120 248 Z[0] Z[1] Z[2] 5 6 1 364 368 372 z ip 364 372 564 772
pointers.c – step by step x y int x=1, y=2, z[10]={5,6,7}; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ printf("ip now points to x that contains the value %d\n",*ip); y = *ip; /* y is now 1 */ printf("y is now %d\n",y); *ip = 0;/* x is now 0 */ printf("x is now %d\n",x); ip = &z[2];/* ip now points to z[2] */ printf("ip now points to z[2] that contains the value %d\n",*ip); *ip = 1; /* z[2] is now 1 */ printf("z[2] is now %d\n", z[2]); printf("ip is %p\n", ip); 0 1 120 248 Z[0] Z[1] Z[2] 5 6 1 364 368 372 z ip 364 372 564 772
Common errors • It is impossible to define pointers to constants or expressions. • It is also impossible to change a variable’s address (because it is not for us to determine!). • Therefore, the following are errors: • i = &3; • j = &(k+5); • k = &(a==b); • &a = &b; • &a = 150;
Pass arguments by value • The functions we saw till now accepted their arguments “by value” • They could manipulate the passed values • They couldn’t change values in the calling function
Wrong Swap val_swap.c
How can we fix it? • We can define swap so it gets pointers to integers instead of integers void swap(int *x, int *y) { …swap *x and *y… } • We then call swap by swap(&x,&y); • This is passing values by address
Right Swap add_swap.c
Back to scanf • We can now understand the & in scanf(“%d”,&a); • The argument list in scanf is simply passed by address, so scanf can change its content
Exercise Write a function that accepts a double parameter and returns its integer and fraction parts. Write a program that accepts a number from the user and prints out its integer and fraction parts, using this function
Solution dbl_split.c
Exercise • The relation between rectangular and polar coordinates is given by – r = sqrt(x2+y2)θ = tan-1(y/x) • Implement a function that accepts two rectangular coordinates and returns the corresponding polar coordinates • Use the function atan defined in math.h
Solution rec_to_polar.c
Pointers and Arrays • Recall that an array S holds the address of its first element S[0] • S is actually a pointer to S[0] int S[10]; int *P; P=S; /* From now P is equivalent to S */ • Both P and S are now pointing to S[0]
Pointer-array equivalence • Arrays are actually a kind of pointers! • When an array is defined, a fixed amount of memory the size of the array is allocated. • The array variable is set to point to the beginning of that memory segment • When a pointer is declared, it is uninitialized (like a regular variable) • Unlike pointers, the value of an array variable cannot be changed
Passing arrays to functions • Arrays can be passed to functions and have their values changed. • This is possible because an array variable is actually an address. • The two following function argument declarations are equivalent – • int arr[] • int *arr • Example – vec_mul.c
Arrays as function arguments • Functions can accept arrays as arguments • Usually the array’s size also needs to be passed (why?) • For example - int CalcSum(int arr[], int size); • Within the function, arr is accessed in the usual way • Example – calc_sum.c
Exercise • Implement a function that accepts two integer arrays and returns 1 if they are equal, 0 otherwise • Write a program that accepts two arrays of integers from the user and checks for equality
Solution • compare_arrays.c
Exercise • Implement the functionint Subsequence(int arr1[], int size1,int arr2[], int size2); • The function returns 1 iff arr2 is a subsequence of arr1 • For example, if arr1 = {1, 4, 6, 8} and arr2 = {4, 6}.
Solution • subsequence.c
Exercise • Write a program that accepts positive integers from the user until a negative one is entered • The program then displays the 5 largest integers entered • Not necessarily sorted by size! • Note – there’s no need to define functions other than main
Solution • top_numbers.c