简体   繁体   中英

function warnings C: “warning: type of ‘char_array’ defaults to ‘int’”

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

int myprint(char_array){
    char mystring[80];
    strcat(mystring, "\n");
    printf("%s", mystring);
    return 0;
}

int main(int argc, char** argv){
    int count = 5;
    char letter = 'c';
    printf("decimal: %d, char: %c\n", count, letter);

    myprint("sup");

    return 0;

}

I get warnings on compile:

cchilders:~/projects/buildyourownlisp_in_C/ch3 [master]$ compile basics.c basics
basics.c: In function ‘myprint’:
basics.c:4:5: warning: type of ‘char_array’ defaults to ‘int’
 int myprint(char_array){
     ^

It compiles, but my myprint function doesn't work:

cchilders:~/projects/buildyourownlisp_in_C/ch3 [master]$ ./basics 
decimal: 5, char: c

I see this answer warning: return type defaults to 'int' [-Wreturn-type] but doesn't apply to me since I did declare int main(...)

I also see this declaration of functions:

return_type function_name( parameter list ) {
   body of the function
}

And for myprint I declare as taking int and return 0 . What does this warning mean and why doesn't my function work? Thank you

ANSWER:

void myprint(char mystring[]){
    strcat(mystring, "\n");
    printf("%s", mystring);

}

quiets the warnings, but causes Segmentation fault (core dumped)

Changing to

void myprint(char[] mystring){
    strcat(mystring, "\n");
    printf("%s", mystring);

}

makes it worse:

cchilders:~/projects/buildyourownlisp_in_C/ch3 [master]$ cc -std=c99 -Wall basics.c -o basics
basics.c:4:21: error: expected ‘;’, ‘,’ or ‘)’ before ‘mystring’
 void myprint(char[] mystring;){
                     ^
basics.c: In function ‘main’:
basics.c:15:5: warning: implicit declaration of function ‘myprint’ [-Wimplicit-function-declaration]
     myprint("sup");
     ^

I also tried

void myprint(char[] mystring;){...

and

void myprint(char[] mystring,){...

You are not providing a data type for char_array in

int myprint(char_array)

You need char * or whatever you want it to be.

Firstly, function definitions should be like

return-type function-name ( parameter-type parameter-name, parameter-type parameter-name)
{ ... }

You did not specify either a parameter type or a parameter name. If you mean char_array to mean a type, you need to define it first, using a typedef or a struct or something else. If you mean char_array to be a parameter name, you need to specify its type, as

char[] char_array

say. Also, in this case, you do not actually use the variable char array anywhere in the function myprint. So the argument "sup" is not being used at all.

After edit to the question:

Try

char str[] = "sup";
myprint(str);

instead. As far as I know, you can't pass a string (a character array) by value.

As others have pointed out, you didn't specify a type for char_array , so it is assumed to be int . Changing it to char char_array[] fixes this.

Your other problem is that you're passing a string constant ( "sup" ) to this function and are then attempting to modify it. String constants are stored in a read-only section of memory, so you can't modify it.

Given that you're only printing the string with a newline, you can do this instead:

void myprint(char mystring[]){
    printf("%s\n", mystring);
}
int myprint(char_array)

What type has the parameter with name char_array ? Because you didn't specify it, the compiler assumed it to be an int . Luckily it's warning you about that.

Don't rely on such behaviour, though. (I don't know whether this is still legal in C11 for example) Just write correct function declarations, including parameter types.

You need to specify the type of the parameters you expect to be passed to the function. There are mistakes in the function too. char_array is of char* type. You need to copy every part of the passed array to your local array, THEN only can you call printf for this function to work

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