![](/img/trans.png)
[英]Why am I not using '->' correctly? I know it is probably a simple conceptual mistake, but don't know why
[英]i don't know why it appears this mistake
我編寫了一個代碼,用於在c中打印矩陣:
int main(int argc, char** argv) {
int i,j,columna,n,m;
char sala_cine[X][Y];
sala_cine[X][Y]=
{
{' ',1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20},
{'1',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'2',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'3',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'4',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'5',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'6',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'7',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'8',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}
};
for (i=0;i<X;i++){
for(j=0;j<Y;j++)
printf("%c",sala_cine[X][Y]);
printf("\n");
}
return (EXIT_SUCCESS);
}
並且在sala_cine[X][Y]=
行中出現此錯誤:
expeted expression before '{' token.
您能幫我解決問題嗎?
謝謝
您不能像這樣在C中分配數組。 但是在您的情況下,您可以在聲明時初始化:
int main(int argc, char** argv) {
int i,j,columna,n,m;
char sala_cine[X][Y] =
{
{' ',1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20},
{'1',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'2',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'3',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'4',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'5',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'6',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'7',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'8',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}
};
for (i=0;i<X;i++){
for(j=0;j<Y;j++)
printf("%c",sala_cine[i][j]);
printf("\n");
}
return (EXIT_SUCCESS);
}
另外,打印的for循環有一個錯誤,如@Eugene指出
使用C11,代碼可以將復合文字分配給sala_cine
,它現在是一個指針。
sala_cine = (char [X][Y]) { ... } // see below
注意:print (char)
將打印控制字符,而不是數字'1'
。
#define X 9
#define Y 21
int main(void) {
int i,j;
char (*sala_cine)[Y];
sala_cine = (char [X][Y])
{
{' ',1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20},
{'1',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'2',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'3',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'4',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'5',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'6',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'7',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
{'8',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}
};
for (i=0;i<X;i++){
for(j=0;j<Y;j++)
// printf("%c",sala_cine[X][Y]);
// Change from X,Y to i,j;
printf("%c",sala_cine[i][j]);
printf("\n");
}
return (EXIT_SUCCESS);
}
在C語言中,您不能一步一步聲明一個數組,以后再對其進行初始化。 (您嘗試做的):
// WRONG
char sala_cine[X][Y]; // Declaration Step
sala_cine[X][Y]= { .... }; // Initialization Step
相反,您必須立即執行以下操作:
// Correct
char sala_cine[X][Y] = { .....}; // Declaration and Initialization at once.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.