I just startet learning C and I am stuck with printing the elements of an array. I want to read a .txt-file and save it to an array. Then print the elements of the array.
The text file says:
1
2
3
4
My C code says:
#include <stdlib.h>
#include <stdio.h>
int main() {
FILE *fp;
char myArray[30];
int wordCount = 0, i;
fp = fopen("list.txt", "r");
if(fp != NULL) {
while(!feof(fp)) {
fscanf(fp, "%s", &myArray[wordCount]);
wordCount++;}
}
printf("%s\n", myArray);
printf("%s\n", &myArray[1]); // intend to print 2nd element
printf("%d\n", arrayOfWords[1]); // intend to print 2nd element
return 0;
}
The output says:
1234
234
50
Neither &myArray[1]
nor myArray[1]
works.