0
int generate(int **p, int M, int N, int K)
{
    int i, j, m=0;
    *p = (int**)malloc(M * sizeof(int*));
    if (*p == NULL)
    {
        printf ("xxx");
        exit(0);
    }
    for (i=0; i < M; i++)
        (*p)[i] = (int*)malloc(N * sizeof(int));
        if ((*p)[i] == NULL)
        {
            printf ("xxx");
            exit (0);
        }
    srand(time(NULL));
    while (m < K)
    {
        i = rand()%(M);
        j = rand()%(N);
        if (*p[i][j] != '*')
        {
            *p[i][j] = '*';
            m++;
        }
    }
}

Compiler rings on 5 different lines where a *p is located. I just want to create a 2dimensional dynamically alocated matrix and send it to the address(&) of a *pointer on the main code through this function. I stated the function as int instead of void and on the main code i am simply calling it and not inserting it in a variable. If these have anything to do with the errors please tell me, thank you! Also compiler error: "comparison between pointer and integer", "assignment makes integer f pointer without a cast".

goros god
  • 1
  • 2
  • 3
    By the way your function returns nothing though its return type is not void. – Vlad from Moscow Jan 21 '21 at 14:14
  • Missing a star in the argument list. Need `int generate(int ***p, int M, int N, int K)`. – Adrian Mole Jan 21 '21 at 14:14
  • What's the address **type** of a `**int` variable? – Andrew Henle Jan 21 '21 at 14:15
  • @goros god In this statement *p = (int**)malloc(M * sizeof(int*)); the left side hand operand has the type int * while the right hand side operand has the type int **. – Vlad from Moscow Jan 21 '21 at 14:16
  • Also, you're not allocating a 2-dimensional array. You're allocating a one-dimension array of pointers to multiple and separate one-dimensional arrays of `int` values. See [**Correctly allocating multi-dimensional arrays**](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) if you want to allocate true 2-dimensional arrays. – Andrew Henle Jan 21 '21 at 14:17

0 Answers0