I’m using this function to remove newline characters from a string:
void remove_newline(char *string) {
string[strcspn(string, "\r")] = "\0";
string[strcspn(string, "\n")] = "\0";
}
The interesting thing is that I’m getting a warning when I try to compile this:
warning: assignment makes integer from pointer without a cast
Why I’m getting such warning in this situation?
"\0"is a string literal and defines a character array of{'\0', '\0'}. It decays to a pointer if used without index. This pointer in turn then you are trying to assign tostring[...]which is an 8 bit integer, achar, hence the warningTo code a single character, a character literal, use single quotes:
'\0'