Stefan
Stefan

Reputation: 395

Compiler error: "format ‘%f’ expects argument of type ‘double’"

The program takes a 9-word long code (For example: 011112222). The first number is the code for the operation (0 for +, 1 for -, 2 for *, 3 for /) the next 4 numbers are the first number and the other 4 are the other number so for 011112222 the result should be 1111+2222. This is my code:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

float* Calculate(char* buffer)
{
    int number1, number2;
    
    char *firstPart = (char *)malloc(strlen(buffer));
    char *secondPart = (char *)malloc(strlen(buffer));

    strncpy(firstPart, buffer+1, 4);
    strncpy(secondPart, buffer+5, 8);

    number1 = atoi(firstPart);
    number2 = atoi(secondPart);

    float *result;

    if( buffer[0] == '0')
    {
        *result = (float)number1 + (float)number2;
    }
    else if(buffer[0] == '1')
    {
        *result = (float)number1 - (float)number2;
    }
    else if(buffer[0] == '2')
    {
        *result = (float)number1 * (float)number2;
    }
    else if(buffer[0] == '3')
    {
        *result = (float)number1 / (float)number2;
    }

    return result;
}

int main()
{
    char buffer[20];
    printf("Insert code word: ");
    scanf("%s", buffer);

    float *result = Calculate(buffer);
    printf("%f", result);
    
    return 1;
}

But I get this error:

In function ‘main’:
    zadatak.c:54:14: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float *’ [-Wformat=]
        printf("%f", result);

What is causing this warning and how can I resolve it?

Upvotes: 2

Views: 4500

Answers (2)

dbush
dbush

Reputation: 225767

In your printf call:

printf("%f", result);

The %f format specifier expects a double (or a float which gets automatically converted), but you're passing it a pointer to a float. You need to dereference the pointer to get the float value:

printf("%f", *result);

However, you have another problem.

Inside of Calculate you define result, which is what you return from the function, as a pointer to a float, but you never set it to point anywhere. So anytime you try do dereference this pointer you're reading an uninitialized value and using it as a valid pointer. Doing so invokes undefined behavior.

There's no reason to use a pointer to a float in your code, so change both result in main and result in Calculate from float * to float, and change Calculate to return a float.

Also, the strncpy function doesn't always null terminate the destination string, and malloc returns uninitialized memory, so atoi will can read past the end of allocated memory when it reads the strings. You can fix this by using calloc instead of malloc which returns memory initialized to all 0's. Also, be sure to free those buffers when you're done with them.

float Calculate(char* buffer)
{
    int number1, number2;

    char *firstPart = calloc(strlen(buffer), 1);
    char *secondPart = calloc(strlen(buffer), 1);

    strncpy(firstPart, buffer+1, 4);
    strncpy(secondPart, buffer+5, 8);

    number1 = atoi(firstPart);
    number2 = atoi(secondPart);

    free(firstPart);
    free(secondPart);

    float result;

    if( buffer[0] == '0')
    {
        result = (float)number1 + (float)number2;
    }
    else if(buffer[0] == '1')
    {
        result = (float)number1 - (float)number2;
    }
    else if(buffer[0] == '2')
    {
        result = (float)number1 * (float)number2;
    }
    else if(buffer[0] == '3')
    {
        result = (float)number1 / (float)number2;
    }

    return result;
}

int main()
{
    char buffer[20];
    printf("Insert code word: ");
    scanf("%s", buffer);

    float result = Calculate(buffer);
    printf("%f", result);

    return 1;
}

Upvotes: 4

Horacio Goetendia
Horacio Goetendia

Reputation: 203

Your code needs many validations and changes to be reliable, but i made few changes to reach your initial purpose.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

float Calculate(char* buffer)
{
    int number1, number2;

    char *firstPart = (char *)malloc(strlen(buffer));
    char *secondPart = (char *)malloc(strlen(buffer));
    float result;

    strncpy(firstPart, buffer + 1, 4);
    strncpy(secondPart, buffer + 5, 4);
    number1 = atoi(firstPart);
    number2 = atoi(secondPart);


    if( buffer[0] == '0')
    {
        result = (float)number1 + (float)number2;
    }
    else if(buffer[0] == '1')
    {
        result = (float)number1 - (float)number2;
    }
    else if(buffer[0] == '2')
    {
        result = (float)number1 * (float)number2;
    }
    else if(buffer[0] == '3')
    {
        result = (float)number1 / (float)number2;
    }

    free(firstPart);
    free(secondPart);

    return result;
}

int main()
{
    char buffer[20];
    printf("Insert code word: ");
    scanf("%s", buffer);

    float result = Calculate(buffer);
    printf("%f", result);

    return 1;
}

Upvotes: 0

Related Questions