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

前端 未结 6 658
小鲜肉
小鲜肉 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:04

    I have a note for rendon's answer:

    For his code, Visual C++ says for every "=" operations: error C2440: '=' : cannot convert from 'void *' to 'int **'

    By making some changes, it works for me, but I'm not sure that it does the same, so I afraid of editing his code. Instead, here's me code, that seems to work for a first impression.

    int **a;    
    
    a = (int **)malloc(rows * sizeof(int));
    
    for (i = 0; i < rows; i++)
    {
        a[i] = (int *)malloc(cols * sizeof(int));
    }
    
    for (j=0;j

    Actually, I did it with a custom struct instead of ints but I think either way should it work.

提交回复
热议问题