Nested structures and how to initialise

C

Captain Pugwash

Hi!

What's the correct way to initialise nested structures?

I have, for example, the following:

struct staff_member
{
char *forename;
char *surname;
};

struct department
{
char *name;

struct staff_member *staff;
};

struct department departments[] =
{
{ "development", { { "Alex Buell" } } },
{ "marketing", { { "Sales". "Droid 1" }, { "Sales", "Droid 2" } } },
};

This won't compile correctly, What is the correct way to do this?
Thanks
 
C

Captain Pugwash

struct department departments[] =
{
{ "development", { { "Alex Buell" } } },
{ "marketing", { { "Sales". "Droid 1" }, { "Sales", "Droid 2" } } },
^^^ Is this just a type for ',' ?

Yes, it's a typo, sorry.
The initialisation value for struct staff_member *staff must be a pointer, not a
struct. If you want it to indeed be pointer, you need to initialise with
something like

struct staff_member developmentdepartment[] = {
{ "Alex Buell" } ,
};
struct staff_member marketingdepartment[] = {
{ "Sales". "Droid 1" }, { "Sales", "Droid 2" },
};
struct department departments[] = {
{ "development", developmentdepartment },
{ "marketing", marketingdepartment },
};

So if I use pointers, I have to do it in this way?

What if I have a array of structs in there? I don't know how many
members of staff there will be in each department. Can I do it the same
way as with 'struct department departments[]'?

Thanks, you've helped enormously.
 
C

Captain Pugwash

I think so.

Right, ok thanks.
What if I have a array of structs in there? I don't know how many
members of staff there will be in each department. Can I do it the same
way as with 'struct department departments[]'?

You can do some tricks with [1] sized arrays and allocating remaining elements
after the struct, but I think the above approach might be simpler and more
robust.

I think you might be right. Thank you for your help!
 
S

Seebs

What's the correct way to initialise nested structures?

You are not trying to initialize nested structures.
struct staff_member
{
char *forename;
char *surname;
};

struct department
{
char *name;

struct staff_member *staff;
};

You have declared a pointer.
struct department departments[] =
{
{ "development", { { "Alex Buell" } } },
{ "marketing", { { "Sales". "Droid 1" }, { "Sales", "Droid 2" } } },
};

You are trying to initialize an array.
This won't compile correctly, What is the correct way to do this?

Make up your mind, first, as to whether you want these to be arrays
or pointers. Note that, as you've written this, even if it worked it
wouldn't work, because you'd have no way to know how many staff_member
objects there were in the first department.

Oh, also, you have a period where you need a comma. But it doesn't matter;
a bracket-enclosed list of objects can initialize an array, but not a
pointer. (The usual madness about char *s and char []s both taking string
literals is a special case.)

-s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,954
Messages
2,570,114
Members
46,702
Latest member
VernitaGow

Latest Threads

Top