Functions and Pointers Dr. Sajib Datta CSE@UTA
Passing multi-dimensional array #include <stdio.h> void print(int [][3], int , int); int main(void) { int a[2][3] = {{1,2,3}, {4,5,6}}; printf("%d\n", sizeof(a)); print(a,2,3); return 0; } void print(int b[][3], int r, int c) int i,j; for(i=0;i<r;i++) for(j=0; j<c;j++) printf("%d", b[i][j]); printf("\n");
Pointers
Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example using the address of a variable scanf("%d", &year);
Addresses in Memory Preceding a variable name by an ampersand, known as the address operator, will return its address: #include <stdio.h> int main(void) { int x; /* notice the format specifier in printf() for an address is %p */ printf("The address for the memory allocated to x is %p\n", &x); return 0; }
hexadecimal (or called base-16) 0123456789ABCDEF The address for the memory allocated to x is 0012FF60 in the previous example. For 32 bits system the 0012FF60 hexadecimal will be converted to 32 bits binary (one hex character represented by 4 bits).
Pointer A pointer is a variable whose value is a memory address. Note the type of a pointer indicates the type of variable which it points to. E.g., int * (called pointer-to-int type) should be initialized to point to a variable of type int. Similarly, we have char*, float *, …… Given a pointer variable, assignment can be done by using: int * ptr = &pooh; /*assigns pooh’s address to ptr, we say ptr points to pooh*/ ptr = &bah; /*make ptr point to some other variables*/
#include<stdio.h> int main(void) { int num = 3, num1 = 5; int* numptr; /* numptr is a pointer */ printf("content of num is %d\n", num); printf("address of num is %p\n", &num); printf("address of num1 is %p\n", &num1); numptr = # /* initialize numptr with the address of num */ printf("content of numptr is %p\n", numptr); numptr = &num1; return 0; }
#include<stdio.h> int main(void) { int num = 3, num1 = 5; int* numptr; /* numptr is a pointer */ printf("content of num is %d\n", num); printf("address of num is %p\n", &num); printf("address of num1 is %p\n", &num1); numptr = # /* initialize numptr with the address of num */ printf("content of numptr is %p\n", numptr); numptr = &num1; return 0; } Output: content of num is 3 address of num is 0012FF60 address of num1 is 0012FF54 content of numptr is 0012FF60 content of numptr is 0012FF54 Press any key to continue . . .
Why pointers C was developed when computers were much less powerful The raw ability to work with particular memory locations was obviously a useful option to have. Programming microcontrollers still need this. Optimize a program to run faster or use less memory that it would otherwise.
#include<stdio.h> int main(void) { int arr[3] = {2, 4, 6}, i; for(i = 0; i<3; i++) printf("The address of %d element is %p.\n", i, &arr[i]); return 0; } The address of 0 element is 0032FE08. The address of 1 element is 0032FE0C. The address of 2 element is 0032FE10.
Indirection operator Pointers allow us to modify content in memory by using indirection operator (or called dereference operator). Putting an asterisk before your pointer variable See the example in the next slide
What’s going here. #include <stdio What’s going here? #include <stdio.h> int main(void) { int bah = 10, val; int* ptr = &bah; val = *ptr; printf("The value of val is %d.\n", val); return 0; }
Pointers We can use pointers in much the same way we do the variables that they point to. #include <stdio.h> int main(void) { int a = 3, b = 3; /* a and b start with equal values */ int* bptr = &b; /* we’ll modify b using a pointer */ a *= 4; *bptr *= 4; printf("a is %d, b is %d\n", a, b); a--; (*bptr)--; /* parentheses are necessary here to override the order of precedence */ return 0; }
Output: a is 12, b is 12 a is 11, b is 11
Define multiple pointers together Wrong! If you use int * a, b to define two integer pointer variables. Instead, you should use int * a, *b; #include<stdio.h> int main(void) { int * a, *b;//it’s wrong if int *a,b int c = 10; b = &c; a = &c; printf("b: %p and a: %p\n", b, a); return 0; }
Pointers to Pointers Pointers can contain the address of another pointer. int main(void) { int num = 5; int* numptr = # int** ptr2 = &numptr; /* notice the two asterisks */ printf("num is %d\n", num); printf("*numptr is %d\n", *numptr); printf("The content in numptr is %p.\n", numptr); printf("*ptr2 is %p\n", *ptr2); printf("**ptr2 is %d\n", **ptr2); return 0; }
Output num is 5 *numptr is 5 The content in numptr is 0012FF60