0

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.

Romaldowoho
  • 405
  • 1
  • 7
  • 20
  • Show us how you allocate `words`. And you are referencing 2D array wrong. To access 2D array, you should do `words[#row][#column]`. Consider this question http://stackoverflow.com/questions/15258084/how-to-get-column-of-a-multidimensional-array-in-c-c – Isa A Sep 17 '15 at 04:09
  • @IsaA my rows are MAX_WORD and my columns are NUMWORDS. I think its right? – Romaldowoho Sep 17 '15 at 04:16
  • the thing is, you are trying to print address to your actual data by accessing `words[#index]`. There should be a lot of reference to how you should reference your 2d array. see http://www.programiz.com/c-programming/c-multi-dimensional-arrays , also, first show us how you allocate your array – Isa A Sep 17 '15 at 04:19
  • You probably filled the array wrong. Also can you explain what you are trying to print exactly? Your existing code tries to print every possible pair of 2 words (sort of) – M.M Sep 17 '15 at 04:31
  • Apparently you seem to confuse the concept of string in C. If you are trying to put a `word` in each slot of `words`. You have to define `words` as `char ***words`. Unless you mean you want to put one character in each slot. – Isa A Sep 17 '15 at 04:58
  • I do want one char in each slot. – Romaldowoho Sep 17 '15 at 05:18
  • @Romaldowoho Sorry, I misunderstood your question. See my updated answer. – Isa A Sep 17 '15 at 05:36

3 Answers3

1

You allocate memory for words as

words = (char**) malloc(sizeof(char*)*NUMWORDS);

What is done by you is allocate memory for each words pointer, but address where character sit was not allocated. So you can done this by a loop

for(int i=0; i<NUMWORDS; i++)
{
    words[i] = (char*)malloc(sizeof(char)*MAX_WORD);
}

Your print segment has also some fault, here code I have tried, may be what you wanted.

    char **words;
    words = (char**)malloc(sizeof(char*)*NUMWORDS);
    for(int i=0; i<NUMWORDS; i++)
    {
        words[i] = (char*)malloc(sizeof(char)*MAX_WORD);
    }
    strcpy(words[0], "Hello world");
    strcpy(words[1], "Hi how are you?");
    char word[MAX_WORD];
    int k, j;
    for (k = 0; k < NUMWORDS ; k++)
    {
        for (j = 0; words[k][j]!='\0'; j++)
        {
            printf("%c", words[k][j]);
        }
        printf("\n");
    }

For Good practice you can declare two dimensional array like

 char words[NUMWORDS][MAX_WORD];

Or

char *words[NUMWORDS];

And allocate memory for all words by a loop. As your NUMWORDS and MAX_WORD is constant.

ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
0

From your information, probably this is what you want

char **words; 
char word[MAX_WORD]; // this is unnecessary
int k, j;

while (NOT_EOF) { // NOT_EOF is not c, fix this to work
    // allocate a column. remember, this might always fail.
    words[wordscount] = (char *)malloc(sizeof(char) * MAX_WORD);
    // get your word from a file
    fscanf(filestream, "%s", words[wordscount]);
    // increment wordscount 
    wordscount++;
}

// print all characters, one row each line (one string each line)
for (k = 0; k < wordscount ; k++) {
   for (j = 0; j < MAX_WORD; j++) {
      printf("%c", words[k][j]);
   }
   printf("\n");
}

Something like that.

Isa A
  • 1,342
  • 13
  • 31
  • I allocate it in a while loop by word[ i ] and I increment i each time. – Romaldowoho Sep 17 '15 at 04:37
  • because I allocate for each word I get from the file. I am storing the words from a file to a 2D array words. I allocate each slot in words array in the while loop right after I get the word. – Romaldowoho Sep 17 '15 at 04:39
  • @Romaldowoho Then first you have to allocate your `words[i]`, and each of the necessary `words[i][j]` to store your string – Isa A Sep 17 '15 at 04:43
  • I do... Im doing an assignment and I need to print this for debugging, but I still hesitant to just post my code here. – Romaldowoho Sep 17 '15 at 04:46
  • @Romaldowoho Don't be hesitant. And if you are post the minimal code copy and post here . – ameyCU Sep 17 '15 at 05:25
-1
//print array
int k;
for (k = 0; k < MAX_WORD ; k++) {
    printf("%s", words[k]);
    printf("\n");
}
Kelvin Lai
  • 2,209
  • 7
  • 24
  • 26