0

I can't understand why when I'm calculating the num of elements of 'A' in main(), so the result is correct. But when I pass the 'A' to function myfunc() and calaculating there so the result is incorrect.
Can please someone explain me?

typedef struct
{
	char description[30];	
	unsigned price;	
}Car;

void myfunc(const Car A[])
{
        //The result is 0
         int i =  sizeof(A)/sizeof(A[0]);
}
void main()
{
	Car A[]={{"Fiat punto (red)", 160000},
	{"Cadilack",500000},
	{"Subaru 1600 second hand",90000},
	{"Volvo 3000 10 years old",130000},
	{"Mazda 3, 2000, from 2006", 142000}};
    
                //The result is 5
                int i = sizeof(A)/sizeof(A[0]);
}

Thank you.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
0

It doesn't work because on line 7 the array is treated as a pointer, and the size of all pointers in 32-bit compilers is 4. If you want myfunc() to know the size of the array then it will have to be passed as another argument. void myfunc(const Car A[], int size)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.