-1

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.

4 Answers4

2

You are probably looking for a 2 dimensional array:

int field[n][5];

Example:

int main(void){

    int n = 3, i, j, k=0;
    int field[n][5];

    for(i = 0; i < n; i++) {
        for(j = 0; j < 5; j++) {
            field[i][j] = k++;
        }
    }

    for(i = 0; i < n; i++) {
        for(j = 0; j < 5; j++) {
            printf("%3d", field[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

Cmd-$> out.exe
  0  1  2  3  4
  5  6  7  8  9
 10 11 12 13 14
A4L
  • 17,353
  • 6
  • 49
  • 70
1

you can make a 2d array like (declaration) int field[20][20]; then you can use it like (initialization) field[i][j] = 5;

for your example

int n = 33;
int field[34][6];
for (i = 1; i <= n; i++) {
   field[i][5] = 4;
}
1

No, it is not possible in such way. But it can be replaced with something similar:

int (*field_)[m] = calloc( n, m*sizeof(int)); // or malloc(n*m*sizeof(int));
/* a lot of some operations */
free(field_);

So you have n elements: field_[0], field_[1] ... . And all of them have m ints.

Michael
  • 1,505
  • 14
  • 26
  • there's no need for `malloc()` unless you're using a prehistoric compiler that doesn't support C99 VLAs (or you are planning to return the array - but even then, a true 2D array would be better than a scattered pointer-to-pointer). –  Nov 13 '13 at 11:46
  • @Michael You didn't, it is still a pointer-to-pointer based _lookup table_, not a 2D array. You fragment data all over the heap. [Read this](http://stackoverflow.com/questions/12462615/how-do-i-correctly-set-up-access-and-free-a-multidimensional-array-in-c). – Lundin Nov 13 '13 at 12:01
  • @Lundin Thanks for the link. Fixed it again. Must be alright now. – Michael Nov 13 '13 at 13:16
0

For your basic IO checkout this tutorial: Click Here

#include <iostream>
using namespace std;

int main ()
{
  int n = 0;
  cout << "Please enter number of fields: ";
  cin >> n;
  int field[n];
  for (int i = 0; i < n; i++) {
     field[i] = i;
  }
  //Then to print out what is stored.
  for (int i = 0; i < n; i++) {
     cout << '\n' << field[i];
  }
  return 0;
}

If you input 10 when prompted for number of fields the output would be.

   Please enter number of fields: 10

   0
   1
   2
   4
   5
   6
   7
   8
   9