0

I'm learning C at the moment and tried to write this function

int *create_matrix(int n) {
   int *matrix = malloc(n*n*sizeof(int));
   srand(time(NULL));
   int i, j;
   for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        matrix[i][j] = rand()%10;
      }
   }
   return matrix;
}

why does this fail to compile? its complaining about matrix[i][j] is not an pointer/array. but I've just declared it as an pointer six lines above...

CinCout
  • 9,486
  • 12
  • 49
  • 67
thebackfisch
  • 21
  • 1
  • 2
  • use matrix[i * n + j], you don't have a multidimensional array – Gunther Van Butsele Oct 15 '14 at 11:22
  • 1
    Since you declared `int *matrix`, `matrix` is a pointer, but `matrix[i]` is an int, thus `matrix[i][j]` doesn't make sense (it's like saying `21[i] = rand()%10;`). – Dettorer Oct 15 '14 at 11:37
  • Thank you everyone for all the explaination! I clearly see my mistake now and it seems i'll need a little bit more practice with pointers... – thebackfisch Oct 15 '14 at 20:30

5 Answers5

7

It's a 1D array, so you have to treat it as a 1D array, and not as 2D.

You can of course still store your n x n elements in it:

    matrix[i * n + j] = rand() % 10;

If you prefer, you can set up a 2D structure by following the advice given in How do I work with dynamic multi-dimensional arrays in C?

By the way, you probably don't want to be calling srand() every time you create a matrix. If you call create_matrix() twice in quick succession, you could end up getting the same "random" matrix. Call srand() once at the start of your program.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

You've declared matrix as a one-dimensional array, and not as a two-dimensional array. So use it like:

for(int i=0; i<n*n; ++i)
    matrix[i] = //whatever;

If you really need two-dimensional array, you need to use double pointers:

int **matrix = malloc(n * sizeof(int *));  // notice the double pointer
// ....
// ....
for (i = 0; i < n; i++)
{
    matrix[i] = malloc(sizeof(int) * n);
    // ....
}
CinCout
  • 9,486
  • 12
  • 49
  • 67
  • You also need to allocate the rows if you are going for a two-dimensional approach. – Tom Fenech Oct 15 '14 at 11:34
  • I think that's obvious! I was just suggesting a way how it could be approached. – CinCout Oct 15 '14 at 12:38
  • It's obvious to me and perhaps to you but the issue here is that you've half-suggested another way of doing something, making your answer incomplete. It would be a lot more useful to show the full approach, or simply leave it out entirely as others have already explained it in more depth. – Tom Fenech Oct 15 '14 at 13:22
1

You declare matrix as a one-dimensional array. You need to either store the data in it as such, i.e.

matrix[i * n + j] = rand() % 10;

Or declare it as a two-dimensional array and allocate memory accordingly.

    int** matrix = malloc(n * sizeof(int*));
    int i, j;
    for (i = 0; i < n; i++) {
        matrix[i] = malloc(n * sizeof(int));
        for (j = 0; j < n; j++) {
            matrix[i][j] = rand() % 10;
        }
    }
Aleks G
  • 56,435
  • 29
  • 168
  • 265
1

What it's really complaining about is, the matrix[i] is not a pointer/array. You've only created a 1-dimensional array, you either can access it directly like

matrix[i * n + j] = rand() % 10;

or go ahead, do it cleanly, and redo the array, so it will be 2-dimensional. You'll need to use pointer to a pointer (for every member in line - one row)

int **matrix

but you'll have to loop trough the

*matrix

to malloc each row by itself.

Look up on 2-dimensional arrays.

//EDIT: Also, move the srand() to the beginning of the file. You don't want to srand() with every new matrix.

Kiraa
  • 495
  • 4
  • 11
1

You want two dimensions, then you need a pointer to pointer (not a single pointer):

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int **create_matrix(int n)
{
   int **matrix = malloc(n * sizeof(int *));
   srand(time(NULL));
   int i, j;
   for (i = 0; i < n; i++) {
      matrix[i] = malloc(sizeof(int) * n);
      for (j = 0; j < n; j++) {
         matrix[i][j] = rand()%10;
      }
   }
   return matrix;
}

int main(void)
{    
    int **matrix = create_matrix(5);
    return 0;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94