Day#32 'C' Coding Challenge

Day#32 'C' Coding Challenge

What is the output of the below code?

#include <stdio.h> void main() { int arr[10] = {1,2,3}; printf("%d\n%d\n%d\n",sizeof(arr),&arr,&arr+1); }

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

The above code tests below scenarios:

1. How to calculate the total size of the array
2. Accessing the array address

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

arr[10] is an array of 10 elements
For sizeof(arr), total array size is calculated. As arr has 10 elements and are integer type elements. Each element occupies 4 bytes and the total size of array is 10*4 = 40 Bytes.
&arr returns the starting address of an array
&arr+1 increments the array address by total array size
So, if &arr output is X, then &arr+1 output is X+40

Post a Comment

0 Comments