Cameron Clark, All, 25 Aug 97, 08:51
CC> char string[6];
CC> strcpy(string,"12345");
CC> string[strlen(string)] = '\n';
Somebody already answered your question, but I also wish to note that your
code is broken, because it removes the string's terminating zero.
Apparently, you happened to have a zero right after the last byte of
string[], but you obviously cannot count on it. Also, you only allocated 6
chars for the string, while the full "12345\n" string requires 7 chars (5
numerals, one newline, one zero).
The correct code should look like:
char string[7];
strcpy(string, "12345");
string[strlen(string) + 1] = 0;
string[strlen(string)] = '\n';
Notice that I add the new terminating zero BEFORE changing the old one, so
that the subsequent call to strlen() would be defined.
Of course, even better code would be:
char string[7];
strcpy (string, "12345");
strcat (string, "\n");
Hope this helps,
- Ido
---
---------------
* Origin: Ido. Pronounced ee-DO. (2:403/409.42)
|