In this guide, we will learn how to work with Pointers and arrays in a C program. I recommend you to refer Array and Pointer tutorials before going though this guide so that it would be easy for you to understand the concept explained here.
A simple example to print the address of array elements
#include <stdio.h> int main( ) { int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ; /* for loop to print value and address of each element of array*/ for ( int i = 0 ; i < 7 ; i++ ) { /* The correct way of displaying the address would be using %p format * specifier like this: * printf("val[%d]: value is %d and address is %p\n", i, val[i], &val[i]); * Just to demonstrate that the array elements are stored in contiguous * locations, I m displaying the addresses in integer */ printf("val[%d]: value is %d and address is %d\n", i, val[i], &val[i]); } return 0; }
Output:
val[0]: value is 11 and address is 1423453232 val[1]: value is 22 and address is 1423453236 val[2]: value is 33 and address is 1423453240 val[3]: value is 44 and address is 1423453244 val[4]: value is 55 and address is 1423453248 val[5]: value is 66 and address is 1423453252 val[6]: value is 77 and address is 1423453256
Note that there is a difference of 4 bytes between each element because that’s the size of an integer. Which means all the elements are stored in consecutive contiguous memory locations in the memory.(See the diagram below)
In the above example I have used &val[i] to get the address of ith element of the array. We can also use a pointer variable instead of using the ampersand (&) to get the address.
Example – Array and Pointer Example in C
#include <stdio.h> int main( ) { /*Pointer variable*/ int *p; /*Array declaration*/ int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ; /* Assigning the address of val[0] the pointer * You can also write like this: * p = var; * because array name represents the address of the first element */ p = &val[0]; for ( int i = 0 ; i<7 ; i++ ) { printf("val[%d]: value is %d and address is %p\n", i, *p, p); /* Incrementing the pointer so that it points to next element * on every increment. */ p++; } return 0; }
Output:
val[0]: value is 11 and address is 0x7fff51472c30 val[1]: value is 22 and address is 0x7fff51472c34 val[2]: value is 33 and address is 0x7fff51472c38 val[3]: value is 44 and address is 0x7fff51472c3c val[4]: value is 55 and address is 0x7fff51472c40 val[5]: value is 66 and address is 0x7fff51472c44 val[6]: value is 77 and address is 0x7fff51472c48
Points to Note:
1) While using pointers with array, the data type of the pointer must match with the data type of the array.
2) You can also use array name to initialize the pointer like this:
p = var;
because the array name alone is equivalent to the base address of the array.
val==&val[0];
3) In the loop the increment operation(p++) is performed on the pointer variable to get the next location (next element’s location), this arithmetic is same for all types of arrays (for all data types double, char, int etc.) even though the bytes consumed by each data type is different.
Pointer logic
You must have understood the logic in above code so now its time to play with few pointer arithmetic and expressions.
if p = &val[0] which means *p ==val[0] (p+1) == &val[2] & *(p+1) == val[2] (p+2) == &val[3] & *(p+2) == val[3] (p+n) == &val[n+1) & *(p+n) == val[n+1]
Using this logic we can rewrite our code in a better way like this:
#include <stdio.h> int main( ) { int *p; int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ; p = val; for ( int i = 0 ; i<7 ; i++ ) { printf("val[%d]: value is %d and address is %p\n", i, *(p+i), (p+i)); } return 0; }
We don’t need the p++ statement in this program.
Rajan says
Instead of:
p = &val[0];
Can i write:
p = val[0];
Reason: as for a 2D array val[0] refers to address of 0th element of 2D array, which is a 1D array.
If my reason is incorrect(which i believe is is), please explain the difference between “&val[0]” and “val[0]”, where val is a 2D array
anonymous_helper says
p = &val[0], means you are assigning address of 1st element of array(at 0 position) to variable p, whereas p = val[0] means you are assigning value of 1st element to variable p.
Hope this helps
JCB says
No malloc for your pointer p ?