0

I have a problem with reallocating an array. I want to save inputs to a string array and realloc it with every new entry. Heres my function:

char** history=0;

int historycounter=0;

void saveHistory(char* input){

    history=(char**)realloc(history,sizeof(*history)+sizeof(input)*sizeof(char));
    history[historycounter]=(char*)malloc(sizeof(input)*sizeof(char));
    strcpy(history[historycounter],input);
    historycounter++;
}
slyr
  • 1
  • 3

1 Answers1

1

try this

void saveHistory(const char *input){
    size_t size = strlen(input)+1;
    history = realloc(history, sizeof(*history)*(historycounter+1));
    history[historycounter] = malloc(size);
    memcpy(history[historycounter], input, size);
    historycounter++;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70