Thread: Newb Question concerning sizeof() function

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    2

    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[5];
            int i;
    
            printf("Please enter 5 numbers: ");
    
            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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,111
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    2
    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. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,874
    Quote Originally Posted by Salem View Post
    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. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by stahta01 View Post
    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.
    Last edited by quzah; 07-19-2011 at 02:43 PM.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    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[10] 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)[100];
    	n = 11;
    	
    	int a[10][n];                     // no warning type check gone?
    	// or   int a[10][101];      /// warning 
    	pa = a;
    Last edited by Bayint Naung; 07-20-2011 at 06:31 AM.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by AndrewHunter View Post
    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.
    Quote Originally Posted by Bayint Naung View Post
    Not sure what's the type of VLA?
    I seem to recall its type is "variable length array of <type> of the specified length"
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-23-2010, 12:43 AM
  2. Newbie Question: sizeof() problem in function
    By Xeyide in forum C Programming
    Replies: 3
    Last Post: 09-04-2009, 12:05 AM
  3. Function question for brain-fried newb
    By Karmachrome in forum C Programming
    Replies: 3
    Last Post: 10-28-2005, 05:17 PM
  4. sizeof function
    By chrismiceli in forum C Programming
    Replies: 10
    Last Post: 02-28-2004, 08:28 PM
  5. SizeOf function
    By vanilly in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:18 PM