Search code examples
ccharstackwarningspostfix-notation

Warning (char format, different type arg) when compiling my C program


I haven't learned pointers yet, so I have no idea what other answers were talking about when someone was asking the same question :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);
    }
}

it's a program for doing the same operation as postfix with finite a stack. The other functions are all working ok. When I compile and run on Visual Studio, it works fine, but when I run it on linux (which is used to test my program), it doesn't work. It just gives me: "c:52: warning: char format, different type arg (arg 2)".

I am assuming its the scanf or the atoi function that is causing the problem...

Is there any way to fix this program easily by just changing few letters?


Solution

  • You should not use ampersand (&) when reading a character array. Change: scanf("%s", &input); to scanf("%s", input); and all should be fine.

    input is already a pointer to the begining of the block of memory where the character array will be stored, no need to take its address.