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
2009-09-25 04:20:44
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
2009-09-25 04:23:04
@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
2009-09-25 04:37:45
@Chris while from a technical standpoint that's true, I think the semantics were clear.
phoebus
2009-09-25 04:41:21
+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
2009-09-25 04:22:13