0

I have declared an array in main() and to find the size of an array I can use sizeof() and this gives me the correct result. When the same array is passed to another user defined function and if I use sizeof() then I am not getting expected results. In the code I have attached, I get size of array as 10 in main() but in user defined function, I am getting array size as 2. My question is why its not showing 10? Or is there any other solution for this?

   #include <stdio.h>

   int array(int a[])
  {
   int x=sizeof(a)/sizeof(int);
   printf("\n in user defined() size is %d",x);
   }
  int main()
  {   int a[10],i,x;
      for(i=0;i<4;i++)
      a[i]=i+1;

      x=sizeof(a)/sizeof(int);
      printf("\n in main() size is %d",x);
      array(a);

      return 0;
  }


  output:
 in main() size is 10
 in user defined() size is 2
Ed Heal
  • 59,252
  • 17
  • 87
  • 127

0 Answers0