我正在尝试创建一个int
s 的二维矩阵并使用malloc()
.
我希望矩阵看起来像这样:{{-4,0},{-3,0},{-2,0},{-1,0},{1,0},{2,0},{3,0},{4,0}}
但我希望以后能够更改它,所以我尝试使用malloc()
. 我创建了:
typedef int** scores_table
所以我的函数可以返回类型scores_table
。
这是我的代码:
scores_table alloc_scores_table(){
scores_table scores;
int i,j, *row;
row=(int*)malloc(sizeof(int)*2*8);
scores=(scores_table)malloc(sizeof(int*)*8);
if(scores==NULL || row==NULL){
quit();
}
for(i=0;i<8;i++){
scores[i]=row+i*2;
}
for(i=0;i<8;i++){
for(j=0;j<2;j++){
if(j==1){
scores[i][j]=0;
}
else{
if(i>3){
scores[i][j]=-4+1+i;
}
else{
scores[i][j]=-4+i;
}
}
}
}
return scores;
}
问题是 - 函数只返回-4
,我不知道为什么。我究竟做错了什么?