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

1
  • 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). Commented Sep 29, 2012 at 17:18

6 Answers 6

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';
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

1

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

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.