views:

93

answers:

2

how to add a member variable/attribute to a structure from main in C?

+2  A: 

You can't. C is not a dynamic programming language.

You can, however, define an array in your struct; and allocate memory to that array. Perhaps this can solve your current problem?

Fragsworth
You don't allocate memory to an array, you allocate memory to a pointer. An array is stored directly in the `struct` and is a different beast.
Chris Lutz
@Chris: I see what you're saying, but it isn't all that clear. The answer, perhaps, should say: "You can, however, define a pointer in your structure, and you can dynamically allocate memory for an array and place the pointer to that allocated array in the structure (probably with a counter too)".
Jonathan Leffler
@Chris while from a technical standpoint that's true, I think the semantics were clear.
phoebus
+2  A: 

You can't in any normal fashion. Your structs need to be defined on compile. If you have a bunch of like-items that need to be dynamically added considering setting up a resizeable array as a member.

phoebus