1

If I define a 2D array as:

char *tablechar[][2] = {
      {"0",  "Innova"},
      {"2",  "Brio"  },
      {"3",  "Alto"  },
      {"4",  "Bolero"},
      {"5",  "Swift" }
};

and code in the same file as:

printf("Value = %d\n",sizeof(tablechar));
printf("Value = %d\n",sizeof(tablechar[0]));
printf("Num of rows = %d\n",sizeof(tablechar)/sizeof(tablechar[0]));

Output is as expected:

Value = 40
Value = 8
Num of rows = 5

Now, I pass this 2D matrix in a function whose defination is:

void printstuff(char *tab[][2])
{
   printf("Value = %d\n",sizeof(tab));
   printf("Value = %d\n",sizeof(tab[0]));
   printf("Num of rows = %d\n",sizeof(tab)/sizeof(tab[0]));
}

I get the following output:

Value = 4
Value = 8
Num of rows = 0

with a compiler warning in the above function as:

compiler_warning

Why is it so that the value of sizeof(tab) and sizeof(tablechar) different?

Akay
  • 1,092
  • 12
  • 32
  • 1
    4 is size of pointer. – Dayal rai Mar 08 '16 at 05:53
  • 1
    Possible duplicate of [How to find the 'sizeof'(a pointer pointing to an array)?](http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array) – Kurt Stutsman Mar 08 '16 at 05:54
  • 1
    [Sizeof an array in the C programming language?](https://stackoverflow.com/questions/1975128/sizeof-an-array-in-the-c-programming-language?) – kaylum Mar 08 '16 at 05:54

3 Answers3

4

tablechar is an actual array, but the argument tab will be treated as a pointer to elements of array having the specified type, so sizeof(tab) will be size of pointers while sizeof(tablechar) will be size of the array.

By the way, passing data having wrong type to printf() invokes undefined behavior. You will have to use %zu instead of %d for printing size_t, to which the sizeof operator will evaluated.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Is there any way that I can take a 2D array formal parameter in a function that can be deciphered as an array itself, not a pointer? – Akay Mar 08 '16 at 10:02
2

tablechar is an array of arrays of char pointers while the function parameter tab is a pointer to an array of 2 char pointers.

void printstuff(char *tab[][2])  

is equivalent to

void printstuff(char *(*tab)[2])   

sizeof(tab) will give the size of pointer not the array tablechar. In C, there is way to pass an array to a function. When you pass an array to a function then as per C rule array decay to pointer to its first element.
Always remember that arrays are not pointers.

haccks
  • 104,019
  • 25
  • 176
  • 264
1

for:

char *tablechar[][2] = {
      {"0",  "Innova"},
      {"2",  "Brio"  },
      {"3",  "Alto"  },
      {"4",  "Bolero"},
      {"5",  "Swift" }
};

The type of tablechar is a array which have five elements, the type of each element is char* [2]. So sizeof(tablechar) = 5 * sizeof(char* [2]) = 5 * 8 = 5 * 4 * 2.

The type if tablechar[0] is char* [2], so sizeof(tablechar[0]) = sizeof(char*) * 2 = 4 * 2 = 8.

for

void printstuff(char *tab[][2])
{
   printf("Value = %d\n",sizeof(tab));
   printf("Value = %d\n",sizeof(tab[0]));
   printf("Num of rows = %d\n",sizeof(tab)/sizeof(tab[0]));
}

The type of tab is char**, so sizeof(tab) = 4. The type of tab[0] is char* [2], so sizeof(tab[0]) = 8.

BlackMamba
  • 10,054
  • 7
  • 44
  • 67