您好,我在程序中使用此变量时遇到一些问题:
typedef char chessPos[2];
我到底应该如何扫描它,在函数中使用它,以及如何正确地制作和使用这种类型的数组?
为了扫描它,我尝试使用:
scanf("%c%c", currPos[0], currPos[1]);
但是我收到这个警告:
scanf: format string %c requires an argument of type 'char*', but variadic argument 1/2 has type 'int'
谢谢您的帮助!
这是一种方法:
chessPos currPos;
if (scanf("%c%c", currPos, currPos + 1) != 2) abort();
这是另一种方式:
chessPos currPos;
int i;
if ((i = getchar()) == EOF) abort();
currPos[0] = i;
if ((i = getchar()) == EOF) abort();
currPos[1] = i;
您的原始代码不起作用,因为
%c
格式说明符需要 char*
,即指向 char
的指针,而 currPos[1]
是值。我们需要它的地址,例如currPos + 1
或同等的,&currPos[1]
。