2

Say I have this code

char *string = "";
string += 'A';
string += 'B';
string += 'C';
printf("%s\n", string);

It just prints an empty line. Why does it do this and is there an easy way to concatenate single characters starting from an empty string if I don't know how long it'll be?

  • `string += 'A';` is wrong for a lot of reasons. What you need to do is look into `malloc`and `realloc` (and completely forget about using `+=` for this) – UnholySheep Apr 25 '20 at 20:12
  • To concat two strings use `strcat`, if you want to set a string char by char, start with a big enough buffer and set each char one by one – Ôrel Apr 25 '20 at 20:12
  • Are you coming from Java? – Jens Apr 25 '20 at 20:17
  • @UnholySheep I've changed it to use calloc on the char pointer. now that I have that, How could i set the different memory address allocated to the characters I'm reading in? – Eli Hedrick Apr 25 '20 at 20:21
  • @Jens yea I'm coming form Java – Eli Hedrick Apr 25 '20 at 20:21
  • Ok, C is very explicit. Strings don't grow automatically. You need to allocate enough room and then call functions that append to strings, like sprintf, strcat and others. There's no syntactic sugar, like `+` for strings. – Jens Apr 25 '20 at 20:24

4 Answers4

4

In statements like this

string += 'A';

there is used the pointer arithmetic. The value of the internal representation of the character 'A' is added to the value of the pointer string and as a result the pointer has an invalid value because it does not point to an actual object.

You need to declare a character array as for example

char string[4] = "";

and then you can set respective elements of the array to character literals like for example

int i = 0'
string[i++] = 'A';
string[i++] = 'B';
string[i++] = 'C';
string[i] = '\0';

printf("%s\n", string);

Also you have a typo in this call

printf("&s\n", string);

If a character array already contains a string like

char string[4] = "AB";

and you want to append a character to the end of the string then either you can write using a character literal

size_t n = strlen( string );
string[n] = 'C';
string[n + 1] = '\0';

Or you can use a string literal and the standard C function strcat like

strcat( string, "C" );

In any case the character array shall have enough space to accommodate a new character.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

string is just a pointer to the string literal "", so when you add a char with +, you're actually just moving the pointer instead of concatenating to the string. In C, you can allocate a sufficiently large string and use strcat to add strings into it:

char string[100] = "";
strcat(string, "A");
strcat(string, "B");
strcat(string, "C");
printf("%s\n", string);

If you want to use chars, then you can convert the char to a string first.

Aplet123
  • 33,825
  • 1
  • 29
  • 55
1

string += 'A'; does not append a character to the string, it increments the char pointer string by the value of 'A' which on systems that use ASCII is 65 and makes string point well beyond the end of the "" string literal. Hence this code has undefined behavior.

printf("&s\n", string); should print &s and a newline.

Assuming you mistyped your code in the question, printf("%s\n", string); would have undefined behavior, and printing an empty line is possible, as well as a crash or any other nasty side-effect.

If you want to construct a string one character at a time, use this:

char buf[20];
char *string = buf;
*string++ = 'A';
*string++ = 'B';
*string++ = 'C';
*string = '\0';    // set the null terminator
printf("%s\n", buf);

Conversely, you can use strcat with string literals:

char string[20] = "";
strcat(string, "A");
strcat(string, "B");
strcat(string, "C");
printf("%s\n", string);
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

It just prints an empty line.

You are unfortunate, but not surprisingly so, that your code's undefined behavior manifests as printing an apparently-empty string. It would have been more indicative of the nature of the problem if it threw a segfault, or some other variety of memory-related violation, as would be entirely appropriate.

Why does it do this

Because you are performing arithmetic on the pointer, not modifying the thing to which it points. This expression ...

string += 'A';

... computes the pointer addition of string with the integer character constant 'A' (whose numeric value is system dependent, but is often the ASCII code for capital letter A), and stores the resulting pointer in string. This makes string point to something different than it previously did. It does not in any way modify the contents of the memory to which string pointed.

and is there an easy way to concatenate single characters starting from an empty string if I don't know how long it'll be?

If you have an upper bound on how long the data can be, then the easiest thing to do is declare a large-enough array to contain the data and initialize it to all-zero ...

char string[MAX_LEN + 1] = {0};

You can then add a single character by by writing it to the next available index (which index you may either track, or compute at need via strlen()):

unsigned next_index = 0;

string[next_index++] = 'A';
string[next_index++] = 'B';
string[next_index++] = 'C';

Note that this relies on the zero-initialization -- which is not automatic for local variables -- to ensure that the array contents at all times comprise a null-terminated string. Having done that, you can print the expected result:

printf("%s\n", string);

If you did not know in advance a reasonable upper bound on how long the string may be, or if the upper bound were exceedingly large, then you would need to relying on dynamic memory allocation and reallocation. This is a subject for you to defer until later.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157