char *pear = "";
int f=0;
while(f != 20) {
pear[f] = 'a';
f++;
}
So I want to append a's to the char string Why is this causing a buffer problem And I can't use the strcat I have don't like this.
char *pear = "";
int f=0;
while(f != 20) {
pear[f] = 'a';
f++;
}
So I want to append a's to the char string Why is this causing a buffer problem And I can't use the strcat I have don't like this.
Having a string initialized as
char * pear = "";
prohibits it from being modified. In contrast,
char pear [] = "";
allows you to modify byte 0 (but not subsequent bytes) afterwards without getting an error. However, since the last byte in the string needs to be 0, it is not a good idea to overwrite it.
More importantly, you are trying to give values up to 20th element - you need space for at least 21 elements. Also, be careful with the terminating character - you need the last element in the array to be 0 for it to be a string. Right now it seems that you are just trying to write characters into the array without terminating it properly.
If you don't know the size of your array up front, you can use dynamic memory allocation: malloc, realloc (and don't forget to free at the end).