I’m trying to terminate a string early based on some arbitrary condition, by doing this:
void end_string_early(char string[], int len) {
int i;
char j;
for (i=0;i<len;i++) {
j = string[i];
if (arbitrary_condition(j)) {
string[i] = "a";
}
}
}
I get the compile error:
warning: assignment makes integer from pointer without a cast
What confuses me, is if I do the exact same thing with an int array (changing the value to be another int, instead of a) then it works perfectly. Perhaps it’s something with how the argument is being passed? (Though I was under the impression all arrays were passed by ref) I’m not entirely sure and this is my first go at C (working through K&R book).
'a'and"a"are two different things: the first is the character a, the second is a literal string containing only the character a.You’re trying to assign a literal string to a character.
"a"is a literal string, i.e. achar *, and an element of yourchar[]is achar: an integral type, hence the message about trying to assign a pointer to an integer. The message is a bit confusing becausecharis an integral type.So use:
But this doesn’t terminate the string. For that you’ll need: