struct - How to Create an array/structure before the Main() and initialize it in C -
i know in c, 1 way solve "initializer element not constant" error create strcuture within main() function. suppose have array of structs , want utilize global array. how can create , initialize it?
struct *b = malloc(10*sizeof(struct a)); // want maintain malloc void init_a_types(struct a* t) { t->elm1=0; t->elm2=1; } ... int main() { (k=0;k<10;k++) init_a_types(b+k); ... homecoming 0; }
if want array, why don't declare array?
struct { const char *str; int n; }; struct b[3] = { { "foo", 1 }, { "bar", 2 }, { "baz", 3 } };
if want global pointer, use global pointer:
struct *b; int main() { b = malloc(sizeof(*b) * 10); // stuff free(b); homecoming 0; }
c struct initialization global
No comments:
Post a Comment