Thread: Pass an entire struct

  1. #1
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49

    Pass an entire struct

    Hey guys ive search around on previous, and found some similar post but not exactly. Im trying to pass an entire array of structs to a function. The structure is declared as a local varriable in main ( my professor has a problem with global varriables) and it needs to be pass to a function.

    I was thinking something like this....

    Code:
    
    void option1(struct *data);
    
    int main()
    {
         struct my_struct{
              int x;
              char y;
          } data;
    
     option1( data);
      .
      .
      .
         return o;
    }
    void option1(struct *data)
    {
     // function stuff
    }
    or do i have to pass each element of the struct by itself.

    Code:
    void option1( int data.x, char data.y);

    thanks
    chris

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    to pass an array of structs you pass it just like any other array, that is you pass a pointer to the first element:
    Code:
    void do_smt(struct data *);
    ...
    struct data arr[LEN];
    do_smt(arr);
    :wq

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Code:
    #include <stdio.h>
    
    struct bob{
            int x;
    };
    
    void pass(struct bob arg[50]);
    
    int main(void){
            struct bob new[50];
            int x;
            pass(new);
            for(x=0;x<50;x++){
                    printf("%d\n",new[x].x);
            }
            return 0;
    }
    
    void pass(struct bob arg[50]){
            int x;
            for(x=0;x<50;x++){
                    arg[x].x=1;
            }
    }
    Of course I use 'x' too much

  4. #4
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    OK makes since dont you have to use the arrow opperator to acces a particular elemet of the struct inside the function??

    such as...
    Code:
    arg[i]->x
    also could you use a type def to pass through the function.

    such as

    Code:
    void function1( //i dont know the parameter// data)
    
    
    
    typedef struct my_struct data;
    maybe??
    Last edited by cdonlan; 11-27-2004 at 01:53 PM.

  5. #5
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    no, since arg is an array of structs, not an array of pointers to structs.
    :wq

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    only if it is a pointer to a struct.

  7. #7
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    Man you guys are fast.

    Thanks a bunch

    chris

  8. #8
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    im still getting some warrnings

    Code:
    void option2(char *cat, struct data  *cars[50]);
    
    int main()
    {
    struct data{
    		char rank[2];
    		char name[80];
    		char state[12];
    		char year[5];
    		char category[2];
    		char cost[10];
    	};
    
    	struct data cars[50];
    
         option2(cars);
    
         return;
    }
    
    void option2(char *cat, struct data  *cars[50])
    {
    //function stuff
    }
    thanks

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well you prototyped the function to take a pointer to a char and an array of pointers to structs but pass it only a pointer to a struct.

  10. #10
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    ok i fixed that, but its still not working.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Post the new code and the exact warnings/errors

  12. #12
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    check PMed it to ya thantos.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I checked the code and sent it back to you. Please note that its a bad idea to PM another member your code without asking first. I asked you to post it so that I or another member could comment on it and everyone would have the chance to learn.

    Since you said you didn't want your code posted I won't but allow me to comment on it.

    Arrays are not pointers but they can degenerate into pointers. So when you pass an array to a function that wants a pointer there is no need for the &. Example

    Code:
    char arr[100];
    /* Improper scanf() */
    scanf("%s", &arr);
    /* Proper scanf() */
    scanf("%s", arr);
    Another note:
    Code:
    char *blah[10];
    Declares a variable called blah that is an array of 10 pointers to characters

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  2. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  3. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. My final data does not display
    By p1c1078 in forum C Programming
    Replies: 3
    Last Post: 04-23-2003, 06:32 AM