How to store string into 2D Array in C

Hello!

I am trying to store words in a .txt file into a 2D array of type char. What am I doing wrong? Because whenever I run the program, my main debug sequence won't even execute. That said, the words are not even stored in side my 2D array 'Words.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void FetchWords(){

    int counter=0;
        Words = (char **) malloc ( sizeof(char*) * SizeOfList() );

            for(counter = 0; counter < SizeOfList() ; counter++)
                Words = (char *) malloc ( sizeof(char) * 10);   ///Maximum word size of the word is 9 characters.

///Copying Data
FILE *read;
    char charScanner[256];


    /// Oppening Text File to Read data
    if ((read=fopen("WordList.txt","r"))== NULL){
       printf("Error! opening file");
       exit(1);         /* Program exits if file pointer returns NULL. */
   }

    ///Reading words
    while( fscanf(read, "%s", charScanner) != EOF ){

               fscanf(read, "%s\n", charScanner );

// Main Debug sequence
               #ifdef DEBUG
               printf("%s \n", charScanner);
               #endif

               Words[counter] = charScanner;
               counter++;

    }

      /*  #ifdef DEBSD
        printf("Before printing...\n");
        printf("Words[0] = %s \n" , Words[0]);
      #endif ///DEBUG */  

            fclose(read);

}




My entire code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>

    #define DEBUG

///************** GLOBAL VARIABLES**********************
char **Words;
    char **NineLetterWords;
///********************************************

///Finds out the size of the word list.
int SizeOfList(){

    FILE *read;
        int counter=0;
            char charScanner [256];

    /// Oppening Text File to Read data

    if ((read=fopen("WordList.txt","r"))== NULL){
       printf("Error! opening file");
       exit(1);         /* Program exits if file pointer returns NULL. */
   }


    while( fscanf(read, " %s ", charScanner) != EOF )
        counter++;


   /* #ifdef DEBUG
    printf("In SizeOfList: counter = %d.\n", counter);
    #endif */

    fclose(read);

        return counter;

}


void FetchWords(){

    int counter=0;
        Words = (char **) malloc ( sizeof(char*) * SizeOfList() );

            for(counter = 0; counter < SizeOfList() ; counter++)
                Words = (char *) malloc ( sizeof(char) * 10);   ///Maximum word size of the word is 9 characters.

///Copying Data
FILE *read;
    char charScanner[256];


    /// Oppening Text File to Read data
    if ((read=fopen("WordList.txt","r"))== NULL){
       printf("Error! opening file");
       exit(1);         /* Program exits if file pointer returns NULL. */
   }

    ///Reading words
    while( fscanf(read, "%s", charScanner) != EOF ){

               fscanf(read, "%s\n", charScanner );

               #ifdef DEBUG
               printf("%s \n", charScanner);
               #endif

               Words[counter] = charScanner;
               counter++;

    }

        #ifdef DEBSD
        printf("Before printing...\n");
        printf("Words[0] = %s \n" , Words[0]);
        #endif ///DEBUG

            fclose(read);

}

void FetchNineLetterWords(){


}

void UserInput(){

}

void Programme(){

}

int main(){

    #ifdef DEBUG
   SizeOfList();
       FetchWords();
    #endif // DEBUG

return 1337;
}
closed account (SECMoG1T)
a 2D array of type char well this is how you subscript your 2D array
words[index1][index2];//correct way check your line 69
Thank you , I solved the problem!
Topic archived. No new replies allowed.