0

I just started programming in C a few days ago. I am now trying to learn structs.

I have this program and I want to improve it so that my array people is now an array of pointers to structs. I am not sure how to do this.

I also want to modify my insert method, to call malloc to create a new struct and set the correct array element pointing to it.

As far as I know, malloc is dinamic memory allocation but although I've read some guides I'm still unsure on how exactly to use it. Also, after using malloc, what else do I need to change in my program for it to work as before?

2
  • (I already saw this question today.... you or someone else recently posted this code). Commented Oct 24, 2013 at 17:10
  • Yes, it was probably my friend Sarah. She's working on the same exercise as I am. She had some compilation errors which I didn't fortunately :D However, I am unsure how to improve my code. Commented Oct 24, 2013 at 17:12

1 Answer 1

1

If you want people to be an array of pointers, you have to declare it like this:

struct person *people[12];

Remember that declaration follows use and that dereferencing has lower precedence than array indexing; this means that *people[i] is of type struct person, and thus, people[i] is a pointer to struct person.

To initialize each position in people, you call malloc() to make your pointers point to a valid memory location large enough to hold a struct person. It is as easy as:

people[i] = malloc(sizeof(struct person));

When you don't need people anymore, you have to remember to free every memory position you allocated, by calling free(people[i]) for every position i.

I noticed you declared the array to hold 12 structs. This can be dangerous when someone changes the code: it will not work when HOW_MANY is greater than 12. You should declare an array of the same size:

struct person *people[HOW_MANY];

This ensures that your array always has exactly the space needed.

UPDATE: You need to declare insert as receiving an array of pointers instead of an array of structures:

static void insert (struct person *people[], char *name, int age) { ... }

And people[i].name is invalid. Since people[i] is a pointer now, you need to do it like this:

people[i]->name

Or, equivalently, (*people[i]).name.

The same applies to people[i]->age. Remember to change this both in main() and inside insert.

Also, consider passing i to insert instead of using static variables, unless you have a very good reason to do so. Static variables are used for functions with internal state, and for me, insert is not quite the type of function where you'd want that.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer Filipe! I'm guessing I need a for loop to include free in, is that right?
Yep, by the time you don't need people, you'll have to make a loop like for (i = 0; i < HOW_MANY; i++) free(people[i]);. Also, why do you keep static variables inside insert? Any special reason not to pass i to insert?
I've edited the code to what you suggested, but apparently there is a bug in it. I am getting an incompatible pointer type exception.
arrays.c:42: warning: passing argument 1 of ‘insert’ from incompatible pointer type arrays.c:21: note: expected ‘struct person *’ but argument is of type ‘struct person **’ arrays.c:52: error: request for member ‘name’ in something not a structure or union arrays.c:53: error: request for member ‘age’ in something not a structure or union

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.