Thread: sizeof(arrays)

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    55

    sizeof(arrays)

    My main goal is to find out the number of elements in an array. In my program I have something like this:
    Code:
    int arr1[] = {3,4,6,7};
    
    printf("The array has %d elements.\n", sizeof(arr1)/sizeof(int));
    Which works fine. The problem is when I pass the array to a function of mine and use the same print statement. the result is always 1;

    the function looks something like this:
    Code:
    int nth(int[], int[]);
    
    
    int nth(int arr1[], int arr2[]{
        printf("The array has %d elements.\n", sizeof(arr1)/sizeof(int));
    
        return arr1[0];
    }
    What am I missing here? Thanks...

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    15
    You can't pass arrays as parameters in C. What is actually happening is that you are passing a pointer to the first element of the array (the brackets are just syntactic sugar). You will need to pass the size as another parameter.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    When you pass an array to a function, it automatically decays to a pointer type (IE: int*). So, in your case, I assume sizeof(int*) equals sizeof(int), and you therefore get the result of 1.

    The closest you could get to what you want is a macro, which also lets you abstract the type of the array:

    Code:
    #define NTH(A)   (sizeof(A) / sizeof((A)[0]))

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Quote Originally Posted by rt454 View Post
    You can't pass arrays as parameters in C. What is actually happening is that you are passing a pointer to the first element of the array (the brackets are just syntactic sugar). You will need to pass the size as another parameter.
    Ya I considered passing the size but I was hoping I could get around it to keep the parameter list down. Oh well thanks for the info.

    Ronix, I read about macros like that before but I can't tell exactly what that macro you wrote means. Would you mind explaining it in a little more detail?

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > So, in your case, I assume sizeof(int*) equals sizeof(int)
    Very bad assumption to make in any case, esp with 64-bit being so common. But anywho.

    > Ronix, I read about macros like that before but I can't tell exactly what that macro you wrote means. Would you mind explaining it in a little more detail?
    Even though I'm not Ronix... The macro aspect is not important to portray the concept. Basically it tells you how many elements in a one-dimension array. Since sizeof() returns the amount of "bytes" a specific type/object takes up then you need to divide the total bytes occupied by the array by the size of each array element.

    Ie given an array that occupies 16 bytes (sizeof array) and given each element occupies 4 bytes (sizeof *array or sizeof array[0]) then there are,
    = 16 / 4
    or
    = sizeof(array) / sizeof(array[0]);
    Elements in the array.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Quote Originally Posted by zacs7 View Post
    > So, in your case, I assume sizeof(int*) equals sizeof(int)
    Very bad assumption to make in any case, esp with 64-bit being so common. But anywho.
    Yeah. However, Nextstopearth said that his code yielded a value of 1, which means that sizeof(int*) and sizeof(int) are equal.

Popular pages Recent additions subscribe to a feed