#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
    char *names = NULL;
    int capacity = 0;
    int size = 0;
    printf("Type 'end' if you want to stop inputting names\n");
    while (1) {
        char name[100];
        printf("Input:\n");
        fgets(name, sizeof(name), stdin);
        if (strncmp(name, "end", 3) == 0) {
            break;
        }
        if (size == capacity) {
            char *temp = realloc(names, sizeof(char) * (size + 1));
            if (!temp) {
                if (names) {
                    return 1;
                }
            }
            names = temp;
            capacity++;
        }
        names[size] = name;
        size++;
    }
    for (int i = 0; i < size; i++) {
        printf("OUTPUT :%c\n", names[i]);
    }
    if (names) {
        free(names);
    }
}
I am trying to create an array of dynamic length in C but I don't know what is wrong with my code? I think it is cause of how I take the user input and the problem occurs when the code names[size] = name is executed.
 
     
    