






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The concept of pointers in C programming language, using the analogy of houses and addresses. It covers the basics of pointers, including the address-of operator, dereferencing, and NULL pointers. It also discusses pointer arithmetic and arrays as pointers.
What you will learn
Typology: Lecture notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!
1 int a = 5; // declare & initialize an int to the value of 5 2 int *b = &a; // declare & initialize a pointer to store the address of the variable a
1 int a = 5; // declare & initialize an int to the value of 5 2 int *b = &a; // declare & initialize a pointer to store the address of the variable a 3 int c = *b; // declare & initialize an int to the value of the dereferenced b, which is the value stored by a
- Do addresses have houses of their own? NO! - But when we declare a pointer, we make a house specifically to store an address
1 int paul = 21; // store the value 21 in paul's house 2 int tom = paul; // store the value in paul's house in tom's house (makes a copy) 3 int *melissa = &paul; // store address of paul in melissa's house
1 *melissa = 30;
NULL pointers
1 int *ptr = NULL; // does not point to anything 2 //now we can check if the pointer is safe to dereference (because it actually points to something) 3 if (ptr != NULL){ 4 *ptr = 5; // safe to dereference 5 }
Stress-testing your understanding of pointers:
1 int a = 5; 2 int *ptr = &a; 3 int **ptr2ptr = &ptr;
1 int *p1, *p2, **p3, a = 5, b = 10; 2 p1 = &a; 3 p2 = &b; 4 p3 = &p2; 5 *p1 = 10; 6 p1 = p2; 7 *p1 = 20; 8 **p3 = 0; 9 printf("%i %i %i %i %i\n", *p1, *p2, **p3, a, b); 10 Answer: 11 0 0 0 10 0
Arrays and pointers
1 /* two equivalent function definitions */ 2 int func( int *paramN); 3 int func( int paramN[]);
1 *(arr+j) // access element using pointer arithmetic 2 arr[j]; // access element using [] operator
- Notice the sizeof() was not explicit, the compiler will automatically multiply j by the
1 int *ip; 2 int a[10]; 3 ip = &a[3];
1 ip2 = ip + 1;
1 #include <stdio.h> 2 3 int main() 4 { 5 int array [5] = { 9, 7, 5, 3, 1 }; 6 7 printf("%p\n", ( void *) &array[1]); // print memory address of array element 1, must cast to void pointer to print 8 printf("%p\n", ( void *) array+1); // print memory address of array pointer + 1 9 10 printf("%d\n", array[1]); // prints 7 11 printf("%d\n", *(array+1)); // prints 7 (note the parenthesis required here) 12 13 return 0; 14 }
1 const size_t arr_len = 7; 2 char name[arr_len] = "Mollie"; 3 int numVowels(0); 4 // initialize the pointer to the beginning of the array
11 printf("a: %d, b: %d\n", a, b); 12 }
- This is clearly useful! - So far, we’ve only been able to return a single data type, but if we can modify parameters in
- This is essentially pass-by-reference behavior - In the notes on arrays, we actually never needed to return the array! For instance:
1 //NOTICE: the asterisk (star) next to int indicates we are returning an array 2 int * add_to_zeroth_element( int arr[], size_t arr_len, int value){ 3 // this is just a dummy array operation, in practice you'll do wonderful and amazing things here 4 arr[0] += value; 5 // NOTICE: return the array, we don't use [] here, just the name of the array. 6 return arr; 7 } 8 9 void add_to_zeroth_element_no_return( int * const arr, size_t arr_len, int value){ 10 // this is just a dummy array operation, in practice you'll do wonderful and amazing things here 11 arr[0] += value; 12 // don't need 13 } 14 15 int main(){ 16 int arr[] = {1,2,3}; 17 // notice the type here has to match the return type of the function. Exactly what's going on here will be covered with pointers.
18 int * result = add_to_zeroth_element(arr, 3, 5); 19 20 for (j = 0; j < 3; ++j) 21 { 22 printf("%d ", arr[j]); 23 } 24 25 // increment once more on the first element, no return 26 add_to_zeroth_element_no_return(arr, 3, 5); 27 28 for (j = 0; j < 3; ++j) 29 { 30 printf("%d ", arr[j]); 31 } 32 }
Exercises
1 #include <stdio.h> 2 int main() 3 { 4 int first, second, *ptr, *qtr, sum; 5 6 printf(" Input the first number : "); 7 scanf("%d", &first); 8 printf(" Input the second number : "); 9 scanf("%d", &second); 10 11 ptr = &first; 12 qtr = &second; 13 14 sum = *ptr + *qtr; 15 16 printf(" The sum of the entered numbers is : %d\n\n",sum); 17 18 return 0; 19 }