Ok -- what I was getting at, is that there's going to be more than one unction that needs access to the array. Unless you plan to populate the array in display() too. Nasty!
So you need to declare the struct in the highest level function that uses it, and expect several functions to have to accept it as an argument. Here's how I'd do it:
Code:
typedef struct
{
stuff
} student_t;
/* This jjust defines a type as I said before. It's useful to have it at the top level so functions can access the fields. */
void display(student_t *s)
{
s->field = stiff;
// do stuff to s.
}
void VeryImportantFunction(void)
{
student_t student;
display&student);
}
The moment the last function ends, the struct will be destroyed..It's called "going out of scope". This isn't a problem for a lot of programs and use cases, but there are obviously many ways around it....
- Make it global.
- Dynocally allocate it -- but then you must still keep an eye on the pointer.
- Define it in main. Then it'll exist until the program ends.
- Erm, sure there's a pile of others too. Depends how badly you want that struct to survive