Thread: passing array of structs to function by reference

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    3

    passing array of structs to function by reference

    Hi All,

    I've been trying to work out how to pass an array of structs to a function. After googling and doing some trial and error I came up with this;

    Code:
    #include <stdio.h>
    
    struct frogs 
    {
        int num_eyes;
        int num_legs;
    }s[3]; //three of em
    
    void build_frogs(struct frogs  * f, int n) //Pass pointer to struct frogs
    {
        int i;
        int eyes[3] = {3,4,5}; //Use these to fill our structs
        int legs[3] = {5,6,7};
    
        for(i=0; i < n; i++) 
        {
            f->num_eyes = eyes[i];
            f->num_legs = legs[i];
            f++;        //pointer address is array???????
        }            //So we should be able to do this!!
    }
    
    void print_frogs(struct frogs * f, int n) //See above
    {
        int i;
        for(i=0; i < n; i++) 
        {
            printf("Num eyes  : %d\n", f->num_eyes);
            printf("Num legs  : %d\n", f->num_legs);
    
            f++;
        }
    }
    
    int main(void) 
    {
        build_frogs(&s, 3); //Pass address of struct
        print_frogs(&s,3);
    
        return(0);
    }
    This compiles and when run seems to work and give the right "answers", however my compiler (GCC) gives the following warnings about incompatible pointer types;

    Code:
    cc  -O -pipe   struct3.c  -o struct3
    struct3.c: In function 'main':
    struct3.c:47:14: warning: passing argument 1 of 
    'build_frogs' from incompatible pointer type 
    [-Wincompatible-pointer-types]
    build_frogs(&s, 3);
                  ^
    struct3.c:11:6: note: expected 'struct frogs *' 
    but argument is of type 'struct frogs (*)[3]'
     void build_frogs(struct frogs  * f, int n) 
          ^
    struct3.c:48:14: warning: passing argument 1 
    of 'print_frogs' from incompatible pointer type
     [-Wincompatible-pointer-types]
      print_frogs(&s,3);
                  ^
    struct3.c:28:6: note: expected 'struct frogs *' 
    but argument is of type 'struct frogs (*)[3]'
     void print_frogs(struct frogs * f, int n)
    ^
    So, having tried various options but only getting the above version to compile, what would be the correct way to declare my functions so as to keep my compiler sweet? A relevant tutorial would be great, as I have not managed to find anything that talks about what I'm trying to do.

    Thanks,
    greadey

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    You define the struct 'frogs' and declare an array called 's' with 3 elements at same time.
    Then, in the main-function, you call your functions with '&s'.
    If you write the array without an index, it will be converted to an pointer to the first element of the array.
    So, 's' is the pointer to that array, but you take '&s' … the address of the pointer to the array, this is wrong.
    You should write:
    Code:
    …
        build_frogs(s, 3); //Pass address of struct
        print_frogs(s, 3);
    …
    As a hint: you should not use global variables and magic numbers.
    Only define the struct (without declare the array) at the global scope, you can declare the array in your main.
    For the size of the array, you can use a #define at the start of your code.

    Code:
    #include <stdio.h>
    #define MAX_FROGS 3
    
    struct frog
    {
        int num_eyes;
        int num_legs;
    }
    …
    int main (void)
    {
        struct frog frogs_array[MAX_FROGS];
    
        build_frogs(frogs_array, 3); //Pass address of struct
        print_frogs(frogs_array, 3);
    
        return EXIT_SUCCESS;
    }
    And to the last: because behind the pointer is an array, you can access the arraymember directly with index.
    Code:
    void print_frogs(struct frog * f, int n) //See above
    {
        int i;
        for(i=0; i < n; i++) 
        {
            printf("Num eyes  : %d\n", f[i]->num_eyes);
            printf("Num legs  : %d\n", f[i]->num_legs);
        }
    }
    Last edited by WoodSTokk; 08-14-2015 at 07:14 PM.
    Other have classes, we are class

  3. #3
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by WoodSTokk View Post
    ...
    And to the last: because behind the pointer is an array, you can access the arraymember directly with index.
    Code:
    void print_frogs(struct frog * f, int n) //See above
    {
        int i;
        for(i=0; i < n; i++) 
        {
            printf("Num eyes  : %d\n", f[i]->num_eyes);
            printf("Num legs  : %d\n", f[i]->num_legs);
        }
    }
    I think you meant: f[i].num_eyes and f[i].num_legs
    "Talk is cheap, show me the code" - Linus Torvalds

  4. #4
    Registered User
    Join Date
    Aug 2015
    Posts
    3
    Aha!!!!!! So simple, thanks WoodSTokk, and thanks for the tips - very much appreciated.
    cheers,
    greadey

  5. #5
    Registered User
    Join Date
    Aug 2015
    Posts
    3
    And yes, migf1 was also correct, thanks :-)

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Ooops, you are right, migf1. Thanks for correction.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  2. Array of structs: function passing
    By warsame in forum C Programming
    Replies: 23
    Last Post: 09-27-2010, 09:34 AM
  3. Warning while passing array by reference to function
    By Jdo300 in forum C Programming
    Replies: 11
    Last Post: 06-10-2008, 01:40 PM
  4. Passing an array as reference to a function?
    By Kylecito in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2006, 02:25 AM
  5. Passing an array of structs to a function
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-03-2001, 12:02 PM