Thread: sizes of arrays within functions

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    47

    sizes of arrays within functions

    In C, i used a define when i first was learning which was something like:

    Code:
    #define LENGTH(array) (sizeof(array)/sizeof(array[0]))
    or something along those lines in order to be able to figure out the length of integer arrays (char arrays are easier).

    I was just curious about passing integer arrays to functions either to read them or modify them, and if I can find out their length.

    For example,

    Code:
    int main(){
    int array[5];
    
    printf( "Length: %d", LENGTH(array) );
    
    foo(array);
    }
    
    void foo( int array[] )
    {
       printf( "Length: %d, LENGTH(array) ); //does this work?
    }
    I was also curious as to the proper way to pass and integer array to a function. Can i pass it as int array[] if I want to read and modify it? Or should I be using int *array? If I use int *array will the LENGTH define even work? Thanks.
    Last edited by tempster09; 01-13-2010 at 05:37 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Once an array is passed to a function, it degrades to a pointer to the array, which means sizeof(array) will only give you the size of the pointer. The way around this is to pass the size of the array to the function.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    47
    that's what i thought, but was hoping there was another answer. That's too bad, i was even thinking of writing a function to do it for me but i would never know when to stop counting positions in an array and probably go past a valid index.....

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    I was also curious as to the proper way to pass and integer array to a function. Can i pass it as int array[] if I want to read and modify it? Or should I be using int *array? If I use int *array will the LENGTH define even work? Thanks.
    In a function declaration, there is no difference between int *array and int array[]; that is to say, the following are identical:
    Code:
    void f(int a[]);
    void f(int *a);
    Both of these actually mean (in this particular instance) “pointer to int”. As such, if you modify what a points to, it will be changed in the calling function. The LENGTH macro will work in the sense that it will give you a value, but the value will still be useless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  2. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 06:48 PM
  3. storage class, arrays, functions and good layout
    By disruptivetech in forum C Programming
    Replies: 4
    Last Post: 12-02-2005, 02:34 PM
  4. functions and arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-14-2002, 09:57 AM
  5. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:48 AM