# Thread: Passing a struct

1. ## 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. 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);`

3. 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. Originally Posted by Drainy
How do I declare a prototype a function like that?
Code:
`void func(team*);`
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.

Popular pages Recent additions