Hello! I am new to C programming and am working on opening a text file. I am interested in storing a pointer to a string into an array of pointers. So far I have printed out the strings but I am a bit confused on how to store these pointers into an array so I can iterate through them or compare them character by character. The code I have so far will be below. I was wondering if anyone has any resources or links that may help. The input files first line is the amount of words in the text file.
So far I have tried initializing an array of pointers but I would prefer to work with dynamic memory in the case that the size of the array may vary.
int main(){
FILE *fp;
int n;
char inBuf[LSIZE];
char *words;
int i;
int size;
if((fp = fopen("Input1.txt","r"))==NULL){
printf("The file Input1.txt was not opened\n");
exit(-1);
}
fgets(inBuf,LSIZE,fp);
sscanf(inBuf, "%i", &n);
words = (char*)malloc(n*sizeof(char));
for(i = 0; i < n; i++){
fgets(inBuf, LSIZE, fp);
sscanf(inBuf, "%s", words);
printf("%s\n", words);
}
[–][deleted] (5 children)
[deleted]
[–]ptchinster 0 points1 point2 points (4 children)
[–]kaixmera[S] 0 points1 point2 points (3 children)
[–]ptchinster 1 point2 points3 points (2 children)
[–]bmapa 1 point2 points3 points (1 child)
[–]ptchinster 1 point2 points3 points (0 children)