2016-11-27 177 views
-3

scoreGrid是得分**,10分数创建*。我试图初始化Score *中的每一个,从名为rank的二进制文件中读取。 Score是一个带int和char [7]的简单结构。“需要作为分配的左操作数[错误]左值”

Score** scoreGrid; 
scoreGrid = (Score**)calloc(1,sizeof(Score*)*10); 
if(!scoreGrid){ 
    endOnScore(); 
} 

int res, i, j; 
for(i=0;i<10;i++){ 
    ((*scoreGrid) + i * sizeof(Score*)) = (Score*)malloc(sizeof(Score)); //[Error] lvalue required as left operand of assignment 
    if(!(*scoreGrid + i * sizeof(Score*))){ 
     endOnScore(); 
    } 
    res = fread((*scoreGrid + i * sizeof(Score*)),sizeof(Score),1,rank); 
    printf("res: %d\n", res); 
    if(res == 0){ 
     free((*scoreGrid + i * sizeof(Score*))); 
     break; 
    } 
} 

我评论,我遇到了错误..有人知道可以吗?也许有更好的解决方案?谢谢!

+0

你对索引寻址有什么反应? –

+0

作业左侧的值不是您想要的值。 * scoreGrid与scoreGrid [0]相同 - 返回第一个指针。您应该在解除引用前添加。 – pticawr

+0

使用调试器找到它 – Anjaneyulu

回答

3

((*scoreGrid) + i * sizeof(Score*))相当于scoreGrid[0] + i * sizeof(Score*),这是不是你想要的。

如果你坚持不使用数组访问,你应该写(*(scoreGrid + i)),这相当于scoreGrid[i]

要解决此问题以可读的方式,用scoreGrid[i]取代((*scoreGrid) + i * sizeof(Score*))每发生。

+0

工作得很好!我想我会尝试改变这种坏习惯。谢谢! –

+0

'*(scoreGrid + i)'将相当于'scoreGrid [i]'。 'X [Y]'的定义是'*(X + Y)'。 –

相关问题