Learning C through “Learning C the hard way”, and doing some of my own exercises. I stumbled upon the following problem.
Let’s say I have the following structure:
struct Person {
char name[MAX_INPUT];
int age;
}
In main(), I have declared the following array:
int main(int argc, char *argv[]) {
struct Person personList[MAX_SIZE];
return 0;
}
Now let’s say 2 functions away (main calls function1 which calls function2) I want to save a person inside the array I declared in the main function like so:
int function2(struct Person *list) {
struct Person *prsn = malloc(sizeof(struct Person));
assert(prsn != NULL); // Why is this line necessary?
// User input code goes here ...
// Now to save the Person created
strcpy(prsn->name, nameInput);
ctzn->age = ageInput;
list = prsn; // list was passed by reference by function1, does main need to pass the array by
// reference to function1 before?
// This is where I get lost:
// I want to increment the array's index, so next time this function is called and a
// new person needs to be saved, it is saved in the correct order in the array (next index)
}
So if I return to my main function and wanted to print the first three persons saved in it like so:
...
int i = 0;
for(i = 0; i < 3; i++) {
printf("%s is %d old", personList[i].name, personList[i].age);
}
...
Basically how to reference the array across the application while keeping it persistent. Keeping in mind that main does not necessarily call the function directly that makes use of the array. I’m suspecting someone might suggesting declaring it as a global variable, then what would be the alternative? Double pointers? How do double pointers work?
Thank you for your time.
Here are a few pointers (no pun intended!) to help you along:
As it stands, the line
struct Person personList[MAX_SIZE];allocates memory forMAX_SIZEnumber ofPersonstructs. You don’t actually need to allocate more memory usingmallocif this is what you are doing.However, you could save some memory by only allocating memory when you actually need a person. In this case, you want the
personListarray to contain pointers toPersonstructs, not the structs themselves (which you create usingmalloc).That is:
struct Person * personList[MAX_SIZE];When you create the person:
struct Person * person = (struct Person *) malloc(sizeof(struct Person));personList[index] = person;And when you use the person list:
printf("%s", personList[index]->name);Arrays don’t magically keep a record of any special index. You have to do this yourself. One way is to always pass the length of the array to each function that needs it.
void function1(struct Person * personList, int count);If you wanted to modify the count variable when you returned back to the calling function, you could pass it by reference:
void function1(struct Person * personList, int * count);A possibly more robust way would be to encapsulate the count and the array together into another structure.
struct PersonList { struct Person * list[MAX_SIZE]; int count; }This way you can write a set of functions that always deal with the list data coherently — whenever you add a new person, you always increment the count, and so on.
int addNewPerson(struct PersonList * personList, char * name, int age);I think that much should be helpful to you. Just leave a comment if you would like something to be explained in more detail.