0

There is a simple question.

Here is my code:

#include <stdio.h>
void ArrayLength( int data[] ){
    int len = sizeof(data) / sizeof(data[0]);
    printf( "%d", len );
}
int main() {
  int data[10] = { 3, 5, 1, 2, 6, 6, 10, 29 ,39, 9};
  int len = sizeof(data) / sizeof(data[0]);
  printf( "%d", len );
  ArrayLength(data );
}

Output

10 2

I don't know why the output is 10 2, instead of 10 10?

Kias
  • 47
  • 5
  • Maybe "[How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)" can help. – Uwe Keim Jun 03 '21 at 05:58
  • 3
    How could the `ArrayLength` function possibly know that the `int[]` passed to it had 10 elements? That information is never passed to the function. – David Schwartz Jun 03 '21 at 06:00
  • When you declare an *argument* like `int data[]` the compiler will treat it as `int *data`. And getting the size of a pointer returns the size of the pointer itself, not what it might point to. – Some programmer dude Jun 03 '21 at 06:05

0 Answers0