I would like to ask whether it's possible to create N fields by a for
-loop and to index each of that field by i
. Illustration of what I would like to get (I know it's wrong, just to show the idea):
int n = 33;
for (i = 1; i <= n; i++) {
int field_i[5];
}
-> getting field_1[5], field_2[5],...., field_33[5]
Is there a way in C to get something like that? I'm a beginner, so sorry if it's a really stupid question.
To extend the question (2d array - perfect! That's what I was looking for!) - if I want the user to set the number of fields n, how do I correctly allocate the memory? (simplified version without conditions for scanf and the like)
int n, num;
int *field;
printf ("Number of fields:\n");
scanf ("%d", &n);
for (i = 0; i < n; i++) {
field[i] = (int *) malloc (4 * sizeof( *field))
}
That is wrong I guess? (it doesn't work when I try)
I'm sorry for adding beginners questions, I was trying to find it on google, unsuccesfully.