Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,因此它是on-topic,用于堆栈溢出。
                        
                        4年前关闭。
                                                                                            
                
        
void alloc (int*** matr, int r, int c)
{
int i, j;
*matr=malloc(r*sizeof(***matr));
for (i=0; i<r; i++)
    *matr[i]=malloc(c*sizeof(**matr));

for (i=0; i<r; i++)
    for (j=0; j<c; j++)
        *matr[i][j]=12;
}


函数原型由老师给出。我收到的错误是细分错误。感谢您的回答!

最佳答案

假设matr已初始化,则该语句

*matr=malloc(r*sizeof(***matr)); // ***matr is of "int" type


应该

*matr = malloc(r*sizeof(int *));   // Allocate an array of "r" int *
//matr[0] = malloc(r*sizeof(int *));


for循环中,该语句

*matr[i]=malloc(c*sizeof(**matr));  // **mtr is of "int *" type


应该

(*matr)[i] = malloc(c*sizeof(int)); // Allocate an array of "c" int
//matr[0][i] = malloc(c*sizeof(int));


然后将*matr[i][j]=12;更改为(*matr)[i][j]=12;matr[0][i][j]=12;

请参见工作代码示例:https://ideone.com/hboLKV

10-05 23:44