0

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).

pb2q
  • 58,613
  • 19
  • 146
  • 147
user652650
  • 648
  • 6
  • 18
  • 2
    `"a"` is an array (of length 2) which *decays* to a pointer before the assignment. You want `'a'` (an `int` that can be interpreted as a `char`). – pmg Sep 29 '12 at 17:18

6 Answers6

3

'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. a char *, and an element of your char[] is a char: an integral type, hence the message about trying to assign a pointer to an integer. The message is a bit confusing because char is an integral type.

So use:

string[i] = 'a';

But this doesn't terminate the string. For that you'll need:

string[i] = '\0';
pb2q
  • 58,613
  • 19
  • 146
  • 147
1
string[i] = "a";

must be

string[i] = 'a';

Note the different quotes.


string is an array from chars, and "a" is not a char, it's string literal, containing two chars - one for a and one \0.
So, to set a char element into a char array, you need to use single quotes '.

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
1

"a" is a char[2], which decays to a char* and that's why you're getting that warning; string is a char* so string[i] is a char which means you are trying to assign a char* to a char, which is wrong.

You need to use single quotes:

string[i] = 'a';

Double quotes make an array of char and single quotes make just a single char, so 'a' is a char and assigning a char to a char will work.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
1

Your program will work better with string[i] = 'a'; (instead of "a").

Just in case, be aware that it's not possible to re-assign the chars of a literal string. The piece of code you show does not have this problem, but if you called end_string_early("foo"...), it would.

Community
  • 1
  • 1
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
1

"a" is not a char -- it's a char* (string). You need 'a'.

Linuxios
  • 34,849
  • 13
  • 91
  • 116
1

string[i] = "a" should be string[i] = 'a', "a" is different from 'a', where "a" is a literal string and it "returns a pointer, while 'a' is just a char.

Marcus
  • 6,701
  • 4
  • 19
  • 28