I was having some problem when trying to add element to dynamic char array in C programming. Here is the expected output:
How many characters do you want to input: 5
Input the string:datas
The string is: datas
Do you want to 1-insert or 2-remove or 3-quit?: 1
What is the character you want to insert: a
Resulting string: adata
I already did those user input part in the main function and here is the code in main where I take in the string input, size and pass them to insert():
printf("How many characters do you want to input: ");
scanf("%d", &n);
str = malloc(n + 1);
printf("Input the string class: ");
scanf("%s", str);
case '1':
printf("What is the character you want to insert: ");
scanf(" %c", &input);
insert(str, input, n);
break;
And the part where my insert():
void insert(char *str, char input, int n) {
int i;
size_t space = 1;
for (i = 0; i < n; i++) {
str[i] = (char)(input + i);
space++;
str = realloc(str, space);
if (i > 2) {
break;
}
}
for (i = 0; i < n; i++) {
printf("%c", str[i]);
}
}
However, when I tried to print out the string from insert(), let's say I entered 'a' to append to the first element of dynamic array with a size of 5, the result that I am getting is abcd=
I referenced from the stackoverflow thread and I not sure how to fix this. Thanks in advance.
scanf?scanfreturns a value - how successful it is. This should be checked to ensure that the relevant variable has been "filled in". People make typos and perhaps give them an opportunity to re-enter the datascanf- linux.die.net/man/3/scanfinput+iinto thestrbuffer? Sinceinputisaof course you then getb(a+1),c(a+2), etc. And why continuouslyrealloc? You know exactly how long the final string needs to be. Justmalloconce and be done with it.