Passing an array of Struct elements

Discussion in 'C Programming' started by enliteneer@gmail.com, Jun 25, 2007.

  1. Guest

    I have an array of structures (my own typedef) that I wish to pass to
    a function.
    Since the array with the structs is rather large, I would like to
    pass
    a pointer to just the first element and have the callee function
    recast
    it as an array instead of just a pointer to the first element of the
    array..


    Something like...

    typedef struct
    {
    int structElement;
    }myStruct;

    myStruct myArray[5];

    main()
    {
    foobar( &(myArray[0]);
    }

    and..

    foobar ( myStruct *pElement)
    {
    myStruct recastArray[] = pElement;

    which then, ideally, I could index...
    val = recastArray[2].structElement;
    }

    The problem is, this line:
    myStruct recastArray[] = pElement;
    doesnt compile, citing an 'invalid intializer'. How can I do this?
    , Jun 25, 2007
    #1
    1. Advertising

  2. Flash Gordon Guest

    wrote, On 25/06/07 23:07:
    > I have an array of structures (my own typedef) that I wish to pass to
    > a function.
    > Since the array with the structs is rather large, I would like to
    > pass
    > a pointer to just the first element and have the callee function
    > recast
    > it as an array instead of just a pointer to the first element of the
    > array..


    You need to read section 6 of the comp.lang.c FAQ at http://c-faq.com/

    > Something like...
    >
    > typedef struct
    > {
    > int structElement;
    > }myStruct;
    >
    > myStruct myArray[5];


    Why make this a "global"? Why not declare it in main (or where ever it
    is needed in the real program).

    > main()


    Implicit int was dropped in C99, the latest (but hardly implemented)
    standard. Also it is better to be explicit about lack of parameters.
    int main(void)

    > {
    > foobar( &(myArray[0]);


    You can simplify the above to:
    foobar(myArray);
    You should also ensure a prototype for foobar appears before it is
    called. Either prototype it before main or declare it before main. Then
    you will get better error checking from your compiler.

    > }
    >
    > and..
    >
    > foobar ( myStruct *pElement)
    > {
    > myStruct recastArray[] = pElement;
    >
    > which then, ideally, I could index...


    Easy. Remove the line above any use array indexing on the pointer.
    However, remember that arrays are NOT pointer, and pointers are NOT
    arrays. It is just that attempting to pass an array actually passes a
    pointer to its first element and you can use array indexing on pointers
    and pointer arithmetic on arrays.

    > val = recastArray[2].structElement;
    > }
    >
    > The problem is, this line:
    > myStruct recastArray[] = pElement;
    > doesnt compile, citing an 'invalid intializer'. How can I do this?


    See above.
    --
    Flash Gordon
    Flash Gordon, Jun 25, 2007
    #2
    1. Advertising

  3. Default User Guest

    wrote:

    > I have an array of structures (my own typedef) that I wish to pass to
    > a function.
    > Since the array with the structs is rather large, I would like to
    > pass
    > a pointer to just the first element and have the callee function
    > recast
    > it as an array instead of just a pointer to the first element of the
    > array..


    You're badly confused about arrays and pointers. It's not possible to
    pass an array of any kind, all you CAN do is pass a pointer to the
    first element. However, there's no casting to be done afterwards, as
    you can't cast a pointer to an array.
    >
    > Something like...
    >
    > typedef struct
    > {
    > int structElement;
    > }myStruct;
    >
    > myStruct myArray[5];
    >
    > main()
    > {
    > foobar( &(myArray[0]);


    You can do this, but it's more common to do:

    foobar(myArray);
    > }
    >
    > and..
    >
    > foobar ( myStruct *pElement)
    > {
    > myStruct recastArray[] = pElement;


    This is not possible, pElement is not a correct intializer for an
    array. It's also not needed.

    >
    > which then, ideally, I could index...
    > val = recastArray[2].structElement;


    Skip that, use pElement[2].structElement;

    > }
    >
    > The problem is, this line:
    > myStruct recastArray[] = pElement;
    > doesnt compile, citing an 'invalid intializer'. How can I do this?


    You can't, but don't need to. You COULD, if you really wanted to,
    create an array and copy pElement to it, but that's rather pointless.

    Please read the FAQ on pointers and arrays.

    <http://c-faq.com/>




    Brian
    Default User, Jun 25, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,357
    Chris Fogelklou
    Apr 20, 2004
  2. Mark A. Odell

    Q: volatile struct elements or entire struct?

    Mark A. Odell, Sep 22, 2004, in forum: C Programming
    Replies:
    3
    Views:
    682
    Mark A. Odell
    Sep 23, 2004
  3. beetle
    Replies:
    2
    Views:
    891
    beetle
    Jan 25, 2005
  4. Daniel Rudy
    Replies:
    7
    Views:
    440
    Daniel Rudy
    Mar 31, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    462
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page