【问题标题】:Warning (char format, different type arg) when compiling my C program编译我的 C 程序时出现警告(char 格式,不同类型的 arg)
【发布时间】:2013-01-18 05:00:07
【问题描述】:

我还没有学过指针,所以当有人问同样的问题时,我不知道其他答案在说什么:S...

while(1)
{
    /* intializing variables for the while loop */
    temp1 = 0;
    temp2 = 0;
    val = 0;
    for(counter = 0; counter < 256; counter++)
    {
        input[counter] = ' ';
    }

    scanf("%s", &input);                                            /* gets user input */

    if(input[0] == 'p')                                             /* if user inputs p; program pops the first top element of stack and terminates the loop */
    {                                                               /* and the program overall                                                               */
        printf("%d", pop(stack));
        break;
    }
    if(input[0] == '+' || input[0] == '-' || input[0] == '*')       /* if operator is inputted; it pops 2 values and does the arithemetic process */
    {
        if(stackCounter == 1 || stackCounter == 0)                  /* If user tries to process operator when there are no elements in stack, gives error and terminates */
        {
            printf("%s", "Error! : Not enough elements in stack!");
            break;
        }
        else
        {
            temp1 = pop(stack);
            temp2 = pop(stack);

            push(stack, arithmetic(temp2, temp1, input[0]));
        }
    }
    else                                                            /* if none of the above, it stores the input value into the stack*/
    {
        val = atoi(input);                                          /* atoi is used to change string to integer */
        push(stack, val);
    }
}

这是一个使用有限堆栈执行与 postfix 相同操作的程序。其他功能一切正常。当我在 Visual Studio 上编译和运行时,它工作正常,但是当我在 linux(用于测试我的程序)上运行它时,它就不起作用了。它只是给了我:“c:52: warning: char format, different type arg (arg 2)”。

我假设它是导致问题的 scanf 或 atoi 函数...

有没有什么方法可以通过更改几个字母轻松修复这个程序?

【问题讨论】:

    标签: c char stack warnings postfix-notation


    【解决方案1】:

    在读取字符数组时不应使用 & 符号 (&amp;)。更改:scanf("%s", &amp;input);scanf("%s", input);,一切都应该没问题。

    input 已经是指向将存储字符数组的内存块开头的指针,无需获取其地址。

    【讨论】:

    • 另外,也可以写成&input[0],意思是“指向数组第一个字符的指针”。这相当于单独输入,但可能有助于澄清 scanf 正在传递一个要写入的指针而不是实例变量 - 特别是因为 OP 是指针概念的新手。
    • 我加了一句,请看一下。
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 2012-03-10
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 2013-05-10
    • 2016-05-29
    • 1970-01-01
    相关资源
    最近更新 更多