[格式%c期望使用char *类型的参数,但具有int

问题描述 投票:0回答:1

所以我知道我已经定义了字符。但是有些东西使编译器烦人。

        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是引发错误的原因。

任何帮助都非常感谢

c parsing char scanf
1个回答
0
投票

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

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

即,与%c对应的自变量必须是指向char的指针,就像%d的自变量需要是指向int的指针。

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

© www.soinside.com 2019 - 2024. All rights reserved.