格式%c需要char*类型的参数,但有int



所以我知道我已经定义了char。但是有些东西让编译器很烦恼。

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

它是作为一个字符工作的,但我需要它是一个字符,因为它只是一个字符。

E是我试图扫描到char中的内容,scanf是抛出错误的内容。

任何帮助都很好,谢谢

虽然这不是我可以编译和测试的MCVE,但您可能需要编写

sscanf(line, "%s %c %d %[^n]", location, &direction, &length, name);

也就是说,与%c相对应的自变量需要是指向char的指针,就像%d的自变量需要成为指向int的指针一样。

您收到了一条令人困惑的消息,其中提到了int,因为C的历史怪癖:可变函数参数(格式字符串后的...(仍然使用ANSI C之前的旧规则进行升级。因此,char被扩展为int。这是为了与70年代和80年代的代码向后兼容。

最新更新