所以我知道我已经定义了字符。但是有些东西使编译器烦人。
char direction;
int exit, firstLine, length;
firstLine = 0, exit = 0;
/* Boolean for the whether size has been read. */
while (((fgets(line, sizeof(line), boardFile)) != NULL) || exit == 1)
{
if (firstLine == 0) /* Easy way of handling reading width/height. */
{
/* Split for size and width. */
sscanf(line, "%d,%d", widthPtr, heightPtr); /* Store width and height inside width/height. */
firstLine++;
if (VALIDSIZE(*widthPtr))
{
if (!(VALIDSIZE(*heightPtr)))
{
printf("%d is an invalid Height. Must be between 1 and 12 (Inclusive).", *heightPtr);
exit = 1;
}
}
else
{
printf("%d is an invalid Width. Must be between 1 and 12 (Inclusive).", *widthPtr);
exit = 1;
}
}
else
{
Ship* newShip;
sscanf(line, "%s %c %d %[^\n]", location, direction, &length, name); /* Parse into vars. */
newShip = createStruct(location, direction, length, name); /* Need a createStruct method so it doesn't store the same Struct in the same memory location. */
insertLast(list, newShip); /* Add to the list of structs. */
}
我遇到的错误
format %c expects argument of type char* but argument has type int.
我正在尝试读取此字符串
D4 E 3 NullByte Sub
它曾经是一个char *,但由于它只是一个字符,所以我需要将它变成char。
E是我要扫描到char中的字符,而scanf是引发错误的原因。
任何帮助都非常感谢
答案 0 :(得分:1)
尽管这不是我可以编译和测试的MCVE,但您可能需要编写
sscanf(line, "%s %c %d %[^\n]", location, &direction, &length, name);
也就是说,与%c
对应的自变量必须是指向char
的指针,就像%d
的自变量需要成为指向int
的指针一样。 / p>
由于C的一个历史古怪,您收到一条提到int
的令人困惑的消息:可变参数函数参数(格式字符串后的...
)仍使用ANSI C之前的旧规则进行推广。因此,char
扩大为int
。这是为了与70年代和80年代的代码向后兼容。