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...

3
  • use matrix[i * n + j], you don't have a multidimensional array Commented Oct 15, 2014 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;). Commented Oct 15, 2014 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... Commented Oct 15, 2014 at 20:30

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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);
    // ....
}

3 Comments

You also need to allocate the rows if you are going for a two-dimensional approach.
I think that's obvious! I was just suggesting a way how it could be approached.
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.
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;
        }
    }

Comments

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.

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.