This small testing program is reading strings from the command line, and I'm getting a seg fault. Can anyone help me?
I also want to ask about how is realloc()
different from malloc()
? I mean I think realloc()
is way smarter so why do we even need malloc()
? And for malloc()
I know we have to copy the strings and malloc()
into the new memory, but can anyone gives me an example of that?
#include <stdio.h>
#include <stdlib.h>
//reading the string from the command line
int
main(int argc, char *argv[]){
char** inputStrings;
int i;
for(i=1;i<argc;i++){
inputStrings=realloc(*inputStrings,i*sizeof(argv[i]));
inputStrings[i-1]=argv[i];
}
for(i=0;i<argc-1;i++){
printf("what is in the address: %s\n",inputStrings[i]);
}
free(inputStrings);
return 0;
}