passing structures??

This is a discussion on passing structures?? within the C Programming forums, part of the General Programming Boards category; Hello, I need to know how to pass structures from one function to another as well as the correct syntax ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    passing structures??

    Hello, I need to know how to pass structures from one function to another as well as the correct syntax for the Prototype and function call as well as the function itself..

    Code:
    #include <stdio.h>
    
    struct names{
       char fname[20];
       char lname[20];
       struct name *next;
    }
    
    /*Prototype*/
    void function1(????)
    
    int main ()  {
      struct name *header;
      struct name *current;
    
    header->next=NULL;
    current=header;
    
    function1(??????);
    
    }
    
    void function1(??????)  {
    struct name newnode;
    
    newnode=(struct name *)malloc(sizeof(struct name));
    newnode->next=header->next;
    header->next=newnode;
    
    }

    any assistance would be appreciated.
    Thanks.

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink

    Do you have a good reference book or your text to help you. This is not a very difficult topic.
    Mr. C: Author and Instructor

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Re:

    not really, My book doesn't show how to pass them. it shows everything globally defined...
    I just need to know how to do the prototype, function call and function header... An example of any kind will do... I would just like to see it so I can implement it on a larger scale.


    Thanks.

  4. #4
    incognito
    Guest
    Well here's my crappy attempt of doing this..........


    Code:
    void AddCard (struct Card *NewCard);
    void ViewCardInfo(struct Card *CardInfo);
    
    
    //Struct To add a new card
    struct Card {
    char PlayersName[30];
    char CardYear[10];
    char CardType[15];
    char BirthPlace[30];
    }PlayersCard;
    
    
    
    //something about
    
    AddCard(&PlayersCard); //call function.......
    
    
    
    /***********************/
    
    
    void AddCard(struct Card *PC)
    {
    
    //do stuff here
    
    
    }

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    that was me btw

    incognito

    Was trying to use my FD screen name I guess.
    I point this out so you can know who to flame (me) .

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Code:
    #include <stdio.h>
    
    struct data_struct
    {
      int data_a;
      int data_b;
    };
    
    // Note that you don't pass the struct here, but a pointer to it
    void function1 (struct data_struct *data)
    {
      data->data_a = 1;
      data->data_b = 2;
    }
    
    // Here you pass a struct
    void function2 (struct data_struct data)
    {
      printf ("%d\n", data.data_a);
      printf ("%d\n", data.data_b);
    }
    
    int main ()
    {
      struct data_struct data;
      function1 (&data);
      function2 (data);
      return 0;
    }

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I, and many others, like using typedef for structs.
    Code:
    #include <stdio.h>
    
    typedef struct{
        char name[50];
        char surname[50];
        //blah blah...
    }Data;
    
    int main()
    {
         Data myData = {"The","Dog"};//Don't need the keyword struct
         printf("%s %s", myData.name, myData.surname);
    
         return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-06-2006, 12:06 AM
  3. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 12:22 PM
  4. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 08:15 AM
  5. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM

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