Reputation: 2315
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.
Upvotes: 0
Views: 6658
Reputation: 60007
Here is the code - with the contract that the caller does the free bit! The caller calls it with insert(&str, input, n)
void insert(char **str, char input, int n) {
char* temp = *str;
int i;
*str = realloc(*str, n + 2); /* realloc first */
if(!*str) /* realloc failed */
{
fputs("realloc failed", stderr);
free(temp); /* Free the previously malloc-ed memory */
exit(-1); /* Exit the program */
}
for (i = n; i >= 0; i--) {
(*str)[i + 1] = (*str)[i]; /* Move all characters up */
}
(*str)[0] = input; /* Insert the new character */
printf("%s", *str); /* Print the new string */
}
Sorry about the formatting. That is left to the reader. I have not checked the algorithm but this does not leak memory
Upvotes: 1
Reputation: 20244
You can use
void insert(char **str, char input, int n) {
char* temp = *str;
int i;
*str = realloc(*str, n + 2); /* realloc first */
if(!(*str)) /* realloc failed */
{
fputs("realloc failed", stderr);
free(temp); /* Free the previously malloc-ed memory */
exit(-1); /* Exit the program */
}
for (i = n; i >= 0; i--) {
(*str)[i + 1] = (*str)[i]; /* Move all characters up */
}
**str = input; /* Insert the new character */
printf("%s", *str); /* Print the new string */
}
And pass str
by reference using
insert(&str, input, n); /* Note the '&' */
Upvotes: 1