This code is raising segmentation fault, I'm not entirely where I did wrong. As soon as I input a string, it causes a segmentation fault. I was expecting it to append a new string in a new element of the char array.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char** append(char** b, size_t* size, char target[]);
int main(){
size_t size = 1;
char** b = malloc(sizeof(char)*size);
while(1){
char input[100] = "";
scanf("%99s", input);
if (strcmp(input, "end") == 0)
break;
b = append(b, &size, input);
}
for(int i = 0; i < size; i++)
printf("%s ", b[i]);
return 0;
}
char** append(char** arr, size_t* size, char target[]){
*size += 1;
size_t b = *size;
char** new_arr = realloc(arr, b * sizeof(char));
strcpy(new_arr[b - 1], target);
return new_arr;
}