简体   繁体   中英

C - warning: assignment makes pointer from integer without a cast

I have no clue why I am getting this warning when my variable and return value match. I have pasted below the variable, assignment of that variable to a function and the function itself (return value and all)

char * menuMsg;          /* used to print menu options */

menuMsg = printMenu();

char * printMenu()
{   
    static char message[250] = "Select an option from below:\n";
    strcat(message, "(1) List all files on server\n");
    strcat(message, "(2) Retrieve file from server\n");
    strcat(message, "(3) Retrieve all files from server\n");
    strcat(message, "Enter your selection: ");
    return message;
}

Any clues as to why i would be receiving this error? To me they all line up it terms of type declarations.

This happens because you're calling the function before it's declared. Undeclared functions are assumed to return int , which explains the error.

Move the printMenu() definition to before the function that's doing the call, or add a prototype:

char * printMenu(void);

And make sure you add void , () is wrong for a function accepting no parameters.

Also, your code is broken since each time you call printMenu() it will call strcat() four times, causing the menu text to grow and grow and eventually cause a buffer overrun.

Please include these lines at the top of your code:

#include <string.h>
char * printMenu();

In the future, please include a fully compilable test case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM