Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

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.

like image 755
soccerman stan Avatar asked Apr 14 '26 05:04

soccerman stan


1 Answers

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.

like image 196
unwind Avatar answered Apr 17 '26 02:04

unwind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!