passing structure arrays to functions?

This is a discussion on passing structure arrays to functions? within the C Programming forums, part of the General Programming Boards category; Hi, I have a fairly particular question and I couldn't find the answer in any of my books. I'm trying ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    1

    passing structure arrays to functions?

    Hi, I have a fairly particular question and I couldn't find the answer in any of my books. I'm trying to pass a structure array to a function and I've chosen to access structure members via "->".

    Ex:
    Code:
     return_type function_name(struct X **ar)
         {
            for(i = 0; i < j; i++)
                .
                .
                ar[i]->member = x;
                .
                .
         }
    Theres no problem compiling the interface file. However, I can't seem to pass the structure array to the function correctly in the main program. My syntax is incorrect. I know when passing a structure to a function we use the "&" to pass the address of the structure. But how would you pass a structure array to a function in main() so you can access structure members via ar[i]->member; ?

    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    since its an array i thought you just pass the name of the variable (no & needed). the datatype the array holds doesnt matter, its still an array... or so i thought.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void myfun( struct mystruct array[], size_t arraysize )
    {
        size_t x;
    
        for( x = 0; x < arraysize )
        {
            printf("array[ %d ].something is %d\n", x, array[ x ].something );
        }
    }
    ...
    struct mystruct somearray[ SOMESIZE ];
    ...
    myfun( somearray, SOMESIZE );
    ...
    Like so.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,553
    Well that all depends on
    - how you declare the variable in the calling function
    - how you try and pass the variable in the calling function.

    Given your code, then you would need something like
    Code:
    struct X *foo[10];  // or struct X **foo;
    function_name( foo );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 05:10 PM
  2. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 05:48 PM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  5. Passing data/pointers between functions #2
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 08:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21