Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do I initialize an array of structures in C?

0
Posted

How do I initialize an array of structures in C?

0

Your example_str is an array of 100 pointers to example structures. Initializing all these pointers to NULL, as you’ve done, is fine, but of course none of those null pointers will be usable until you set them to point to an existing struct example, e.g., example[i] = malloc( sizeof(struct example) ). If ex_field_2 only exists inside struct example, then your printf statement is invalid. It should be something like this: for (i = 0; i < 100; i++) printf("%d\n",example_str[i]->ex_field_2… Once again assuming you’ve initialized the example_str[i] pointer. You should think about whether you’re using struct example correctly. It looks like it might be a node in a linked list, since it contains a pointer to a struct example. In this case, you wouldn’t declare an array of pointers to struct example, but build a dynamically allocated linked list.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123