top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: How much space allocated if I declare array as int [][5] vs int [2][5]

0 votes

See the following two program

int main()
  int a[][5] = { 1}; 
  printf("%d", sizeof(a)); 

Output: 20

int main()
  int a[2][5] = { 1}; 
  printf("%d", sizeof(a)); 

Output: 40

Can someone explain the logic why there is such difference of the size?

posted Oct 2, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

It is very simple to understand above two code programs.
In the first program, only a single value is assigned to an array where no. of rows are not defined but the number of columns are defined. If you assigned more than 5 elements in the array, it will consider into second row . Similarly, if number of elements assigned to array are more than ten then elements considered into third row automatically.

While in the second program, array size has already been defined. I meant to say that it is 10 elements in advance. And each integer is 4 bytes long that's why result is 40.

answer Oct 3, 2015 by Harshita
Similar Questions
+1 vote

declare only like arr[10] but here arr[10] is 2-D

+2 votes

I need to get value from the below table

{  {1,25,{45,6,4},5,8,{10,15},1,4,0,0},    

first index is fixed to 3 and in each index no of elements fixed to 10, but inside of some index no of elements is not fixed ... so how can i get this value. how to declare it .

I did like array_x[3][10][];

but its not working .. please suggest

+3 votes

We can declare the array in the following two ways, I want to know what is the re-commanded way -