0

Possible Duplicate:
Why sizeof(param_array) is the size of pointer?

 void print(char arr[]){
  int i;
  printf("%d" , sizeof(arr));  /*print 4**/

}

int main()
{

  char arr[]={0,1,2,3,4};
  printf("%d" , sizeof(arr)); /*print 5**/
  print(arr);
}

When I send the array to the function it seems that the size decrease in 1. What happen?

Community
  • 1
  • 1
user1479376
  • 327
  • 1
  • 2
  • 10
  • Asked not too long ago: http://stackoverflow.com/questions/11622146/why-sizeofparam-array-is-the-size-of-pointer – chris Jul 24 '12 at 17:21
  • When an array is passed to a function, the function arguments represents the pointer to the first element of the array. So when you try to find the size in the function, you get the size of the pointer as a result. – krammer Jul 24 '12 at 17:27

1 Answers1

3

Because you don't really have an array in the function; it has degraded into a pointer and the size is unknown. You are asking for the sizeof a char* which, on your platform, is 4 bytes.

Ed S.
  • 122,712
  • 22
  • 185
  • 265