Malloc compile error: a value of type “int” cannot be used to initialize an entity of type int (*)[30]

前端 未结 6 676
小鲜肉
小鲜肉 2020-12-03 11:56

I must have tried 20 ways of doing this by now. I really need help, no matter what I do i get a error similar to this one.

a value of type \"int\" cannot be          


        
6条回答
  •  执念已碎
    2020-12-03 12:08

    Try this:

    int **array;
    array = malloc(rows * sizeof(int *));
    for (i = 0; i < rows; i++)
      array[i] = malloc(cols * sizeof(int));
    
    // Some testing
    for (i = 0; i < rows; i++) {
      for (j = 0; j < cols; j++)
        array[i][j] = 0; // or whatever you want
    }
    
    for (i = 0; i < rows; i++) {
      for (j = 0; j < cols; j++)
        printf("%d ", array[i][j]);
    }
    

    In your case rows = 160 and cols = 10. Is one possible solution.

    With this approach you can use the two indexes:

提交回复
热议问题