I have a 2D array of chars
char** words;//2D array where each slot holds a word.
char word [ MAX_WORD ];
And I am trying to do a ridiculously simple thing. I need to print the array so I see that I allocated memory right and removed all the newline characters.
Here is what I am trying to do
//print array
int k, j;
for (k = 0; k < MAX_WORD ; k++) {
for (j = 0; j < NUMWORDS; j++) {
printf("%s%s", words[k],words[j]);
}
printf("\n");
}
And this is what I get
����������������=�������������������=�������������������=���
Segmentation fault: 11
I don't have any warnings or compiled errors. I think my problem might be that I am trying to print out a memory address instead of the actual char, or my malloc didn't do what I expected it to do.
This is my first program in C and so far this language pains me in the most uncomfortable way.
This is how I allocate memory for words array
words = (char**) malloc(sizeof(char*)*NUMWORDS);
And then I fill it with words I get from a file input. I malloc for each words while I get the input, store each word at a word array address and then remove \n. I print each word out, so it works.