Implicit Pointers of 2D Arrays in C
Notes:
Implicit Pointers of 2D Arrays in C Programming Language:
- With respect to two dimensional arrays; we must understand some implicit pointers; which help us write efficient, easy to manage and error less codes.
1.
arrayName is implicitly a pointer to the first row of the array
i.e. arrayName returns address of the first row of the array
Ex:
numbers is implicitly a pointer to the first row of the numbers array
i.e. numbers returns address of the first row of the numbers array
2.
&arrayName is implicitly a pointer to the whole array
i.e. &arrayName returns beginning address of the whole array
Ex:
&numbers is implicitly a pointer to the whole numbers array
i.e &numbers returns beginning address of the whole numbers array
3.
&arrayName[rindex] is implicitly a pointer to the given row of the array
i.e. &arrayName[rindex] returns address of the given row of the array
Ex:
&numbers[0] is implicitly a pointer to the first row of the numbers array
i.e. &numbers[0] returns address of the first row of the numbers array
&numbers[1] is implicitly a pointer to the second row of the numbers array
i.e. &numbers[1] returns address of the second row of the numbers array
4.
arrayName[rindex] is implicitly a pointer to the first element in the given row
i.e. arrayName[rindex] returns address of the first element in the given row
Ex:
numbers[0] is implicitly a pointer to the first element in the first row of the numbers array
i.e. numbers[0] returns address of the first element in the first row of the numbers array
numbers[1] is implicitly a pointer to the first element in the second row of the numbers array
i.e. numbers[1] returns address of the first element in the second row of the numbers array
Example Code:
#include <stdio.h>
int main()
{
int numbers[2][2] = {
{0,0},
{0,0}
};
printf("%d\n",numbers); //2293520
printf("%d\n",&numbers); //2293520
printf("%d\n",&numbers[0]); //2293520
printf("%d\n",&numbers[1]); //2293528
printf("%d\n",numbers[0]); //2293520
printf("%d\n",numbers[1]); //2293528
printf("\n");
printf("%d\n",&numbers[0][0]); //2293520
printf("%d\n",&numbers[0][1]); //2293524
printf("%d\n",&numbers[1][0]); //2293528
printf("%d\n",&numbers[1][1]); //2293532
printf("\n");
printf("%d\n",numbers[0][0]); //0
printf("%d\n",numbers[0][1]); //0
printf("%d\n",numbers[1][0]); //0
printf("%d\n",numbers[1][1]); //0
return 0;
}