#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    16
    Rep Power
    0

    How-to: Passing arrays of structures


    Aloha!

    I nedd to pass an array of structures on to a function. How can this be done? Simply by using "new(struct *myarray)" ? Actually, I tought about passing to the function a pointer to that same array, but how do I declare that pointer?

    Thanx in advance!
    :)
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    I assume that you are using C++ (the use of "new" gave it away).

    Yes, to pass an array of structs to a function, you would pass a pointer to that array. However, this is simplified by the fact that the name of that array IS ITSELF a pointer; e.g.:
    Code:
    struct MyStruct  // you can get away with simplified struct declarations in C++
    {
        int  field1;
        int  field2;
    };
    
    MyStruct  someArray[10];   // as a statically declared array
    MyStruct *anotherArray;   // as a dynamically declared array
                                             // before it's used, must be initialized:
                                             //    anotherArray = new MyStruct[10];
    
    void myFoo(MyStruct *elMio);  // one way
    void myOtherFoo(MyStruct elMio[]); // another way
    
    int main(void)
    {
        myFoo(someArray);
        return 0;
    }
    However, if you want to create the array dynamically inside of the function and make that pointer available to the calling function, then you will need to pass a pointer to that pointer:
    Code:
    struct MyStruct  // you can get away with simplified struct declarations in C++
    {
        int  field1;
        int  field2;
    };
    
    MyStruct  someArray[10];   // as a statically declared array
    MyStruct *anotherArray;   // as a dynamically declared array
                                             // before it's used, must be initialized:
                                             //    anotherArray = new MyStruct[10];
    
    void myFoo(MyStruct **elMio,int arraySize)  
    {
        *elMio = new MyStruct[arraySize];
    }
    
    // please note that 
    // void myOtherFoo(MyStruct *elMio[]); 
    //   would mean something different: an array of pointers to MyStruct objects
    
    int main(void)
    {
        myFoo(&anotherArray);
        return 0;
    }
    You could also use a reference to a pointer instead of a pointer to a pointer, but I'm not sure of the exact syntax. You could play around with it if that's what you're trying to do.
    Last edited by dwise1_aol; May 10th, 2003 at 03:09 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "I nedd to pass an array of structures on to a function. How can this be done?"

    No differently than passing an array of int's to a function.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    16
    Rep Power
    0
    Originally posted by dwise1_aol
    the name of that array IS ITSELF a pointer
    OK, this was pretty obvious, I'm sorry I even asked it :rolleyes:

    Can someone point out how to pass a structure to a function? I have this function:

    int ler(int maxWords, char *filename) {...}

    and I need to set it to receive a termios structure; I just can find the syntax anywhere! :(
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    bass20,

    You mean you think there are examples that dwise1_aol didn't provide code for?

    How do you declare a function to accept an int as an argument:

    void func1(int);

    How do you declare a function to accept a reference to an int as an argument:

    void func2(int&);

    How do you declare a function to accept a pointer to an int as an argument:

    void func3(int*);

    Since an array name acts like a pointer you can do this:

    int int_array[20];
    func3(int_array);

    How do you declare a function to accept a structure as an argument(pass-by-value):

    void func4(MyStruct)

    How do you declare a function to accept a reference to a structure as an argument(pass-by-reference):

    void func5(MyStruct&);

    How do you declare a function to accept a pointer to a structure as an argument:

    void func6(MyStruct*);

    Since an array name acts like a pointer, you can do this:

    MyStruct a[20];
    func6(a);

    If its easier for you, write your function declaration for an int type, and then substitute your structure name in place of int.
    Last edited by 7stud; May 11th, 2003 at 10:18 PM.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Of course, if the function is to make any changes to that structure that the rest of the program needs to know about, you'll need to use a pointer or reference (assuming C++) to the structure.

    BTW, if you pass a struct by value, then a copy of it will be created on the stack in the function's stack frame. I've always been uneasy about doing that, so I almost always use a pointer or a reference to the struct. Just a programming habit and personal aesthetic sense.


    Or if you need an example of passing a termios structure itself, then look at the prototype for tcgetattr() or tcsetattr().
    Last edited by dwise1_aol; May 12th, 2003 at 01:11 AM.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    16
    Rep Power
    0
    Man, this is turning into a headache :(
    What I need to do is pass an array of structures on to a function that will print it's contents, so the call by value method will do just fine. From what I could understand from the C (not C++) manual I declared it this way:

    struct {
    char word[31], dia[11], hora[9];
    int ocorre, duracao;
    }palavras[3];

    So I have an array with 4 structures, correct? So can I pass it on simply using, for example:

    void printIt(int maxWords, struct *palavras);


    Or should I do it this way:

    struct {
    char word[31], dia[11], hora[9];
    int ocorre, duracao;
    };

    struct Palavra palavras[3];


    Don't I have to specify the type of the array, or this will do? :confused:

    Thanx in advance!
    Last edited by bass20; May 12th, 2003 at 11:29 AM.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    You're just suffering from some minor confusion. Let's take this on from a strictly C point of view; struct declarations a lot easier in C++, I feel. Actually, I'm a bit weak in this C subject because I was spoiled by C++; I always have to look it up.

    First, if you are going to use the struct later after having declared palavras, then you must give it a name. In C, we give it a "tag name", but when we use that tag name later we always have to use the keyword "struct" with it as well. At the end, I'll quickly cover the typedef, which eliminates the need for using "struct" everywhere.

    So, try this:
    Code:
    struct Palavra
    {
        char word[31], dia[11], hora[9];
        int ocorre, duracao;
    };
    
    struct Palavra palavras[3];
    
    void printIt(int maxWords, struct Palavra *palavras);
    The tag name is Palavra. The keyword "struct" must be used in declaring the array palavras and the struct pointer in printIt().

    Originally posted by bass20
    ...
    So I have an array with 4 structures, correct?
    ...
    No, you have an array with only three (3) structures. Their indices range from 0 to 2. If you want an array with four structures, then you need to declare it as such:
    Code:
    struct Palavra palavras[4];
    Now that's an array of four structures whose indices range from 0 to 3. You knew that the number of elements in an array and the maximum index differed by one, but you just got confused as to which is one less than the other -- the number used in the array declaration is how many elements are in the array; the last element's index is always one less than the total number of elements.

    OK, now for typedef. You can create your own data types with typedef. A very common usage is to rename some of the built-in types to more meaningful names or to give a more complex type definition a single name; e.g.:
    Code:
    typedef unsigned long ulong;
    typedef unsigned int uint;
    typedef unsigned char uchar;
    typedef unsigned char BOOL;
    typedef unsigned char BYTE;         /* unsigned char (8 bit)    */
    typedef unsigned int WORD;          /* unsigned int (16 bit)    */
    typedef unsigned long DWORD;        /* unsigned int (32 bit)    */
    typedef uchar __far *   FPTR ;      /* Far pointer to byte data */
    But where we personally really use typedef a lot is to create a data type for a struct so that we don't have to mess with tag names. In other words, to make it as easy to use as in C++.
    Code:
    typedef struct  /* define a data type for the struct and call it "Palavra" */
    {
        char word[31], dia[11], hora[9];
        int ocorre, duracao;
    }Palavra;
    
    Palavra palavras[4];
    
    void printIt(int maxWords, Palavra *palavras);
    Hope all that helps.

    EDIT:
    BTW, it is also possible to combine the struct declaration with the array declaration. It's just that I customarily use two separate statements to do that. However, you must still use a tag name if you need to reference the struct again later, eg in a function header:
    Code:
    /* both declare struct Palavra and the array palavras in a single statement */
    struct Palavra
    {
        char word[31], dia[11], hora[9];
        int ocorre, duracao;
    } palavras[4];
    
    void printIt(int maxWords, struct Palavra *palavras);
    However, if you use typedef to define a struct type, then you do still need to keep them in separate statements.
    Last edited by dwise1_aol; May 12th, 2003 at 03:00 PM.
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    16
    Rep Power
    0

    Thumbs up


    Thank you for the good, precise and complete info! :) :) :)

IMN logo majestic logo threadwatch logo seochat tools logo