What is the output of the below program?
#include <stdio.h> void main() { int arr1[] = {1,2,3,4,5,6,7}; printf("%d\n%d\n%d\n%d\n",sizeof(arr1[0]),sizeof(arr1[1+1]),sizeof(*arr1),sizeof(arr1)); }
-----------------------------------------------------------------
The above code tests below scenarios:
1. Accessing an array using index
2. What happens when array index is incremented
3. Determining the size of the array pointer
4. Determine the array size
-----------------------------------------------------------------
The array arr1[] is an integer array and is accessed using different options.
- arr1[index] accesses the element of an array. So, arr1[0] is the first element of the array as array index starts with zero
- In arr1[index+number], the array index is increased by number count and corresponding element in the array shall be accessed
- *arr1 is a pointer and points to an address of the array element.
- arr1 accesses the array itself
The actual output of the code is asking for sizeof each points above and the output shall be:
- sizeof(arr1[0]) shall output size of integer and is 4 bytes
- sizeof(arr1[1+1]) shall output size of integer and is 4 bytes
- sizeof(*arr1) shall output size of integer pointer and is 4 bytes
- sizeof(arr1) shall output size of integer array and is (number of elements * 4 bytes) = 7*4 = 28 Bytes
0 Comments