Day#10 'C' Coding Challenge

Day#10 'C' Coding Challenge

 What is the output of the below program?

#include <stdio.h> void main(void) { int (*ptr) [1][2]; printf("%d",sizeof(*ptr)); }

-------------------------------------------

The above code tests two scenarios:

1. Pointer to a multidimensional array
2. Size of pointer based on the declaration
3. using sizeof() function

-------------------------------------------

int (*ptr) [1][2] declares a pointer to the multidimensional array of integer type.
sizeof(*ptr) calculates the size of the entire array and not the pointer size.
An integer has a size of 4 bytes and is always dependent on the type of compiler. The size could be 2 bytes, 8 bytes also
For embedded applications, as we mostly use 32-bit processors and for 32-bit processors the int size is 4 bytes.
So, there are total of 1*2 = 2 elements in the array, each element occupies space of 4 Bytes, so, a total of 8 Bytes is output by the print function.

Post a Comment

0 Comments