Passing a struct

This is a discussion on Passing a struct within the C Programming forums, part of the General Programming Boards category; Hi, Im completely new here so please bear with me if I screw things up! Basically I have a piece ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    24

    Passing a struct

    Hi,

    Im completely new here so please bear with me if I screw things up!

    Basically I have a piece of coursework along the lines of making a football league program in C. Looks easy in theory but ive managed to confuse myself.

    Code:
    typedef struct {
            char name[1][20];
            int p,w,d,l,f,a,t;
    }  team;
    team a = {1,0,0,0,0,0,0,0};
    team b = {2,0,0,0,0,0,0,0};
    team c = {3,0,0,0,0,0,0,0};
    team d = {4,0,0,0,0,0,0,0};
    team e = {5,0,0,0,0,0,0,0};
    team f = {6,0,0,0,0,0,0,0};
    team g = {7,0,0,0,0,0,0,0};
    team h = {8,0,0,0,0,0,0,0};
    team i = {9,0,0,0,0,0,0,0};
    team j = {10,0,0,0,0,0,0,0};
    team k = {11,0,0,0,0,0,0,0};
    team l = {12,0,0,0,0,0,0,0};
    that is the struct ive created with what I believe are declarations for 12 instances of the struct.
    Quite simply, I just cant figure out how to pass a struct into a function.
    For example, I want to pass the struct team into a function which contains a for loop to read in 12 names...
    As far as I can find out I need to pass in an exact defined struct for this to work, eg passing a or b. I cannot pass the struct 'team' and then set a.name to something or b.name to something.

    Is this true or have I confused myself completely? or.. have I confused you all aswell?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    1. You don't need the [1] when declaring your name field of the struct.
    2. You would be better off declaring an array of team structs perhaps called teams to work with.
    3. Your initialization of your team structs is a bit off.

    Code:
    const int NUM_TEAMS = 12;
    
    typedef struct
    {
        char name[20];
        int p, w, d, l, f, a, t;
    } team;
    
    ...
    
    // Example initialization...
    team teams[NUM_TEAMS] = { {"Packers",1,0,0,0,0,0,0}, {"Steelers",2,0,0,0,0,0,0},
                              ... etc ...
                              {"Ravens",11,0,0,0,0,0,0}, {"Cowboys",12,0,0,0,0,0,0} };
    Your function that modifies this array should accept a pointer to type team:

    Code:
    // This sample loops through all the teams and does something
    void func(team* teams)
    {
        int loop;
        for( loop = 0; loop < NUM_TEAMS; ++loop )
        {
            // Inside this loop access the individual teams data by using teams[loop].whatever
            // where whatever is the data member you want to check/access/modify
        }
    }
    You call the function like so:

    Code:
    func(teams);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    24
    Cheers that was perfect!
    Ive managed to understand all that and apply it perfectly, its reading in and outputting just how I wanted.

    Sorry to be a pain but I just had a few more issues.
    How do I declare a prototype a function like that?

    and

    Code:
    void enterteams(team* teams)
    Just to verify I am understanding pointers correctly, that is basically saying that I am passing in the address to a team struct definition inside of the teams array?

    Thanks
    -Steve

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Quote Originally Posted by Drainy
    How do I declare a prototype a function like that?
    Code:
    void func(team*);
    Quote Originally Posted by Drainy
    and

    Code:
    void enterteams(team* teams)
    Just to verify I am understanding pointers correctly, that is basically saying that I am passing in the address to a team struct definition inside of the teams array?
    Is that supposed to be a prototype or a part of the actual function definition? It merely states that teams is a pointer to a memory location that could contain one or more team structs.

    When you actually call the function like so:

    Code:
    team teams[NUM_TEAMS];
    ...
    enterteams(teams);
    That is saying that we are passing the address (a pointer) to the first element (a team struct) of the array teams to the function. Array names used in this manner are taken to mean the address of the first element.
    Last edited by hk_mp5kpdw; 03-14-2005 at 10:05 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. passing struct
    By Doxygen in forum C Programming
    Replies: 2
    Last Post: 10-26-2006, 03:05 AM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. passing struct to function help
    By staticalloc in forum C Programming
    Replies: 4
    Last Post: 10-06-2004, 08:30 AM

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