Friday, 8 December 2017

Understand the 2d Array size

array_2d_size.c

This program is used to understand the 2d array size


#include <stdio.h>


int array_arg_size(int ptrarr[][3])
{
        printf("Vel array_arg_size size of ptrarr[][3] = %d \n", sizeof(ptrarr));





       // Here user will get the below warning
        /*warning as : warning: ‘sizeof’ on array function parameter ‘ptrarr’ will return size of ‘int (*)[3]’ [-Wsizeof-array-argument] */

}

int main()
{
    int arr[4][3] = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}};
    int (*ptr)[3];
//     int ptrarr[][4]; throws error as " error: array size missing in ‘ptrarr’"
  
        printf("Vel sizeof arr[4][3] = %d , (*ptr)[3]= %d,  & NULL = %d \n", sizeof(arr), sizeof(ptr),  sizeof(NULL));

        array_arg_size(arr);
        return 0;
}

/* Output :
       Vel sizeof arr[4][3] = 48 , (*ptr)[3]= 4,  & NULL = 4
       Vel array_arg_size size of ptrarr[][3] = 4  */

No comments:

Post a Comment