My notes on two dimensional arrays in C

2D Array: Array of Arrays.
2D Array is also known as matrix in C

Two ways to initialize 2D Array:
1. int arr[2][3] = { 10, 11, 12, 13, 14, 15};
2. int arr[2][3] = {{10, 11, 12}, {13, 14, 15}};

Second method is more readable.

In 2D Array, it is compulsory to specify the second dimension while declaring the array.

int arr[2][3] = { 10, 11, 12, 13, 14, 15}; //Valid Declaration
int arr[][3] = { 10, 11, 12, 13, 14, 15}; //Valid Declaration
int arr[2][] = { 10, 11, 12, 13, 14, 15}; //Invalid Declaration
int arr[][] = { 10, 11, 12, 13, 14, 15}; //Invalid Declaration

How are 2-D Arrays stored in C?

As computer memory is linear and there are no rows and columns. 2-D Arrays are stored in row-major order. This means the first row is stored first, then second row and so on.

Pointers and 2-D Arrays:

A pointer that points to the 0th element of the array and a pointer that points to the whole array are totally different.

int *ptr; //Pointer to int
int (*ptr1)[5]; //Pointer to an array of 5 integers
int arr[5]; //Array of 5 integers

ptr = arr;
ptr1 = arr;
ptr++;
ptr1++;
When we print the addresses of these pointers, ptr will be incremented by 4 bytes and ptr1 will be incremented by 20 bytes. since the base type of ptr is int * and ptr1 is pointer to array of 5 integers.

In 1-D Array, the name is a constant pointer to the 0th element. In case of 2-D array, 0th element is a 1-D array.

int arr[3][3] = { {0, 1, 2},
                           {3, 4, 5},
                           {6, 7, 8}}

arr points to 0th 1-D array
arr+1 points to 1st 1-D array
arr+2 points to 2nd 1-D array

If address of 0th 1-D array is 1000, address of 1th 1-D array is 1012, address of 2nd 1-D array is 1024.

In general, we can write

arr+i -> Points to ith 1-D array

Note: Dereferencing a pointer to array gives the base address of the array. 

*(arr+i) -> Points to the base address of ith 1-D array

Note: (arr+i), *(arr+i) points to the same address, but their base type is completely different, (arr+i) is pointer to 3 integers, *(arr+i) is pointer to integer.

How to access individual elements of a 2-D array?

*(arr+i) -> Points to the address of the 0th element of the 1-D array,
*(arr+i)+1 -> Points to the address of the 1st element of the 1-D array,
*(arr+i)+2 -> Points to the address of the 2nd element of the 1-D array

So, we can conclude that
*(arr+i)+j -> Points to the base address of jth element of the ith 1-D array.

On, Dereferencing *(arr+i)+j , we will get the value of jth element of ith 1-D array

*(*(arr+i)+j)

Assigning 2-D arrays to a Pointer Variables:

You can assign the name of the array to a pointer variable, but unlike 1-D array you will need pointer to an array instead of pointer to int (int *)

int arr[3][3] = { {0, 1, 2},
                           {3, 4, 5},
                           {6, 7, 8}}
int (*p)[3];
p = arr;
for(i = 0; i < 3; i++)
{
       printf("Address of %d th array %u \n",i , p + i);
        for(j = 0; j < 3; j++)
        {
            printf("arr[%d][%d]=%d\n", i, j, *( *(p + i) + j) );
        }
        printf("\n\n");
}

Passing 2-D arrays as parameters to functions:

Two-Dimensional arrays can be passed as parameters to the function. One important thing for passing multidimensional arrays is, first array dimension does not have to be specified, the second dimensions must be given
E.g.
void display(int ptr[2][3]);
int arr[2][3] = {{1,2,3}, {4,5,6}};
display(ptr);

Fastest way to zero out 2-D array


memset(array, 0, sizeof(array[0][0]) * rows* columns);


Comments

Popular posts from this blog

bb.utils.contains yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig

PR, PN and PV Variable in Yocto