Hi guys,

I am working on a program for my c class and I was wondering anyone knows if it is possible to define a structure that has dynamic members. I think I saw a thread on this already but when I read it, it didn't quite make sense. This is what I am trying to find a way to do.

typedef struct{

	char x;
	char firstName[50];
	char lastName[50];
	int n;
	char phoneNumber[7]; (the amount of phone #'s times n)(so if n is 3, 3 phone #'s)
	

} entry;

I really have no idea on how to do this. When I looked around for tutorials and such I couldn't really find anything relevant. I figure I could accomplish the same thing almost by just setting phoneNumber[100] and putting a space in between each but that would be worst case scenario.

Thanks for taking a look at this.

Hi guys,

I am working on a program for my c class and I was wondering anyone knows if it is possible to define a structure that has dynamic members. I think I saw a thread on this already but when I read it, it didn't quite make sense. This is what I am trying to find a way to do.

typedef struct{

	char x;
	char firstName[50];
	char lastName[50];
	int n;
	char phoneNumber[7]; (the amount of phone #'s times n)(so if n is 3, 3 phone #'s)
	

} entry;

I really have no idea on how to do this. When I looked around for tutorials and such I couldn't really find anything relevant. I figure I could accomplish the same thing almost by just setting phoneNumber[100] and putting a space in between each but that would be worst case scenario.

Thanks for taking a look at this.

Hello y9john,
of course you can have dynamic members in a structure. Can you explain please your question about your structure? You don't know how the telephone numbers will be stored in your char array? I didn't quite get you...

Alex

Hi guys,

I am working on a program for my c class and I was wondering anyone knows if it is possible to define a structure that has dynamic members. I think I saw a thread on this already but when I read it, it didn't quite make sense. This is what I am trying to find a way to do.

typedef struct{

	char x;
	char firstName[50];
	char lastName[50];
	int n;
	char phoneNumber[7]; (the amount of phone #'s times n)(so if n is 3, 3 phone #'s)
	

} entry;

I really have no idea on how to do this. When I looked around for tutorials and such I couldn't really find anything relevant. I figure I could accomplish the same thing almost by just setting phoneNumber[100] and putting a space in between each but that would be worst case scenario.

Thanks for taking a look at this.

Hi, maybe you can accomplish it like this:

typedef struct{
	char x;
	char firstName[50];
	char lastName[50];
	int n;
	char *phoneNumber; // here's a pointer
} entry;

If you need 3 phone #'s, you can malloc an array with 3 items, then make the pointer phoneNumber point to the array.

I was actually thinking about that but I think in c I would have to create a multi-dimensional array to do that wouldn't I?

Hey guys, I actually found out from some of my friends in my class that I dont have to create an array of numbers in the structure. I have to create an array of structures to hold the people and in that structure I have to have a starting phone number. Then a linked list that holds phone numbers. Thank you for all your help I appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.