2013-02-03 17 views
-1

我还没有学过指针,所以我不知道当有人问同一个问题时,其他答案是什么意思:S ...当编译我的C程序时警告(字符格式,不同类型的arg)

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); 
    } 
} 

这是一个程序,用于执行与有限堆栈后缀相同的操作。其他功能都正常工作。当我在Visual Studio上编译和运行时,它工作正常,但是当我在Linux上运行它(用于测试我的程序)时,它不起作用。它只是给了我:“c:52:警告:字符格式,不同类型arg(arg 2)”。

我假设它的scanf或导致该问题的atoi功能...

有什么办法只需更改几个字母很容易地解决这个节目?

回答

0

读取字符数组时,不应使用&符号(&)。更改:scanf("%s", &input);scanf("%s", input);和所有应该没问题。

input已经是指向存储字符数组的内存块开始的指针,不需要取其地址。

+1

此外,还可以写入&input [0],意思是“指向数组中的第一个字符的指针”。这与输入本身是等价的,但可能有助于澄清scanf正在传递一个写入指针而不是一个实例变量 - 特别是因为OP对于指针概念来说是新的。 – SecurityMatt

+0

我已经添加了一个句子,请看看它。 –

相关问题