1

out put is always 8! I am wondering where this number comes from and how can I get the correct size of the size of the array which is 6?

#include <stdlib.h>
#include <stdio.h>

void cal(int a[])
{
    printf("the size of array is= %d\n", (int)(sizeof(a)/(sizeof)(int)));
}

int main ()
{
    int lis[]={1,2,3,4,5,6,7,8,9};
    cal(&lis[3]);
    return 0;
}
Amir Nabaei
  • 1,582
  • 1
  • 15
  • 26

1 Answers1

0

You can not calculate the size of the int array when all you've got is an int pointer. You will always get pointer size.

You can possibly try this(Not sure if that is a good idea):-

#define arrSz(a) (sizeof(a)/sizeof(*a))
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331