3
randomAssign(int **grid, int size){

    int m = rand()%size;
    int n = rand()%size;
    grid[m][n] = 1;

}

int main()
{
    srand(time(NULL));
    int i, j, size;
    scanf("%d", &size);
    int grid[size][size];

    for(i=0; i<size; i++){
            for(j=0; j<size; j++){
                    grid[i][j] = 0;
            }
    }

randomAssign(grid,size); //warning

    return 0;
}

I am getting warning when i call the function. I tried all i can do but i couldn't find the mistake. Where is the mistake? Regards...

Emre dağıstan
  • 179
  • 1
  • 14

1 Answers1

5

Arrays and pointers are different. An array is a series of contiguous elements of a particular type. A pointer is a small object that holds the address of another object.

Your function expects a pointer that points to another pointer. However you tried to supply an array to it. This can't possibly work.

One way to fix your code would be to make the function accept a pointer to an array (not a pointer to a pointer). This could be written:

void randomAssign(int size, int grid[size][size])

This is actually the same as having int (*grid)[size], the first size is redundant (see here for detail) however it serves some documentary purpose.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365