# Thread: Newb Question concerning sizeof() function

1. ## Newb Question concerning sizeof() function

I wrote a simple program that takes in 5 numbers into an array and then spits them back at you. Except that if the number inputted by the user is less than 10, it is doubled.

Simple enough. What I don't understand is why when I run the sizeof() function in my doubleA function, it returns the size of the array as 4. I assume it has something to do with its int value, but the sizeof() works fine in the main().

Halp?

Code:
```#include <stdio.h>

void doubleA(int a[]);

main()
{

int array;
int i;

for(i=0; i<5; i++)
scanf("%d", &array[i]);

// My custom function
doubleA(array);

for(i=0; i<5; i++)
printf("The %d number you printed was %d\n", i+1, array[i]);
// prints sizeof() the array
printf("size of array[] in main() function: %d\n", sizeof(array));
}

void doubleA(int a[])
{

int i, arraysize;
arraysize = sizeof(a)/sizeof(int);
//prints sizeof() the array
printf("size of a[] in doubleA = %d\n", sizeof(a));
for (i=0; i<5; i++)
if (a[i] <10)
a[i] *= 2;
}``` 2. A couple of points.
1. sizeof is an operator (like + or *), not a function.

2. The method you're using to count the elements in the array only works when the actual array declaration is in scope. When you pass an array to a function, all you get is a pointer (so all the sizeof has to work on is a pointer).

The solution (like is used by many standard functions) is to also pass the array size as an additional parameter. 3. Ok, that makes sense. I just tested putting "sizeof(*a)" in the function to see if that helped but it didn't. I guess I'll just accept the rule of thumb to pass the size as a parameter.

Cheers! 4. Originally Posted by Salem A couple of points.
1. sizeof is an operator (like + or *), not a function.

2. The method you're using to count the elements in the array only works when the actual array declaration is in scope. When you pass an array to a function, all you get is a pointer (so all the sizeof has to work on is a pointer).

The solution (like is used by many standard functions) is to also pass the array size as an additional parameter.
Remember sizeof is normally/always an compiler time operator; the value is a constant at run time.

Tim S. 5. Originally Posted by stahta01 Remember sizeof is normally/always an compiler time operator; the value is a constant at run time.

Tim S.
Code:
```#include<stdio.h>
int main( void )
{
int x;
scanf( "%d", &x );
if( x > 0 )
{
int array[ x ];
printf( "array is %d\n", sizeof( array ) );
}
return 0;
}```
:trollface:

Quzah. 6. As always, thanks for keeping everyone honest quzah. VLAs have indeed introduced many interesting things to the mix when you look at compiler implementation, sizeof being one of those things. sizeof is still a unary compile time operator, however it's implementation for VLAs have required some trickery on the part of the compilers. I would be interested to see what solutions some of the makers have come up with.

I understand that the implementation of VLAs in general varies, with what it appears the common solution being simply placing a pointer to the VLA on the stack and then actually placing the array on the heap to avoid stack corruption. I imagine the solution for sizeof would be evaluating everything down to something along the lines of size*sizeof(underyling type) where size gets filled in at run time. 7. sizeof is compile time operator in C89. and in C99 can evaluate VLA size at run-time.
Edit:
As far as I understand, for int a the number of element of part of the type. ie array of 10 integers.
Not sure what's the type of VLA?

Code:
```int n;
int (*pa);
n = 11;

int a[n];                     // no warning type check gone?
// or   int a;      /// warning
pa = a;``` 8. Originally Posted by AndrewHunter I imagine the solution for sizeof would be evaluating everything down to something along the lines of size*sizeof(underyling type) where size gets filled in at run time.
Which is essentially the same way the size of any other array would be computed, except that the length is computed at compile time. Which sort of makes sense. Originally Posted by Bayint Naung Not sure what's the type of VLA?
I seem to recall its type is "variable length array of <type> of the specified length"  Popular pages Recent additions 