1
int array[][2] = {
{1,0}, 
{2,2},
{3,4},
{4,17}
};

int main()
{
    /* calculate array size */

    printf(" => number of positions to capture : %d", (int)(sizeof(array)/sizeof(array[0])));
    func(array);
    return 0;
}
void func(int actu[][2])
{
    /* calculate array size */
    printf(" => number of positions to capture : %d", (int)(sizeof(actu)/sizeof(actu[0])));
 }

Result:

 => number of positions to capture : 4 -- inside main
 => number of positions to capture : 0 -- inside func -- I believe I should get 4 here too

Size of a same arrray in calling and called function are giving different values. Please, help me to find the issue.

yulian
  • 1,601
  • 3
  • 21
  • 49
siva
  • 57
  • 1
  • 5
  • 1
    On of them is taking the size of the array, the other is taking the size of a pointer decayed from an array. – Shahbaz Nov 08 '13 at 15:57

2 Answers2

3

Take a look here and here (note the first link is talking about C++, not C, so while the mechanism is the same, in C there are no such thing as references (&)). The array has decayed into a pointer, so sizeof(actu) is not equal to sizeof(array).

Community
  • 1
  • 1
anjruu
  • 1,224
  • 10
  • 24
2

It's because when arrays are passed as function argument, they are converted to a pointer to the first element.

So in main, sizeof(array)/sizeof(array[0]) gives the array's length 4, as you may expect.

In func, sizeof(actu) is the size of a pointer, typically 4 bytes in 32-bit machine or 8 bytes in 64-bit machine, while sizeof(actu[0] is still two int, which is 8 if int is 4 bytes. In your machine, the pointer is 4 bytes, so integer division 4/8 outputs 0.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294