1

This is a basic question but I am facing problem with array size.

Here, I am printing size of array in both main function and size function. In main function it prints correct size of array as 40 but in size function to whom I have passed array as a argument printing size of array as 8.

I have tried with void size(arr[SIZE] 'or' arr[] 'or' arr[10]) but output remains unchanged.

Can anyone explain why this behaviour?

#include<stdio.h>
#define SIZE 10

void size(int arr[SIZE])
{
        printf("func_size() : size of array is : [%d]\n",sizeof(arr));
}

int main()
{
  {
      int var = 10;
  }
  {
      printf("%d", var); 
  }
  return 0;
}

Output:

func_main() : size of array is : [40]

func_size() : size of array is : [4]

Cœur
  • 37,241
  • 25
  • 195
  • 267
PravinY
  • 500
  • 1
  • 5
  • 9

1 Answers1

2

Its because arrays when passed in functions get decayed into pointers (to the first element) in functions.

sizeof in size() returns the size of the pointer arr. Read about Arrays and Pointers.

Sadique
  • 22,572
  • 7
  • 65
  • 91
  • thanks a lot for answer...! Actually it was giving size as 8 in 64bit m/c and 4 in 32 m/c so got confused. Now I got my concept cleared...! thank you very much. – PravinY Mar 19 '14 at 12:25