Vishnu M.
Vishnu M.

Reputation: 971

Char Pointer Arrays

I was given skeleton code that gives me a char pointer array (wordslist) with a list of words read in from a txt file. I tried to move the values to a 2D array (words). It compiles just fine but when I try to print the array, I'm just getting new lines rather than the strings.

I think the problem might have to do with pointers and specifically at line 55 but I'm not sure what I'm doing wrong.

// Lab 10 ExTalker Skeleton Code

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <ncurses/ncurses.h>


#define MAXWORDS 100
#define WORDLEN 11
#define DEBUG 0   // set to 0 to disable debug output
int readWords(char *wl[MAXWORDS], char* filename); // reads words from the file
// into wl and trims the whitespace off of the end

//modifies s to trim white space off the right side
void trimws(char* s) ;
void printWords(char* words, int wordCount, int rows, int columns);

int main(int argc, char* argv[]) {
char* wordlist[MAXWORDS];
int wordCount;
int i,j,k=0;
int columns=5;
int rows;
wordCount = readWords(wordlist, argv[1]);

if (DEBUG) {
    printf("Read %d words from %s \n",wordCount, argv[1]);
    for (i = 0; i< wordCount; i++) {
        printf("%s,", wordlist[i]);

    }
    printf("\n\n");
}


// most of your code goes here.

if ((((double)wordCount)/columns)>((double)(wordCount/columns))){
    rows=(wordCount/5)+1;
}
else{
    rows=(wordCount/5);
}
printf("%d\n\n", rows);

char* words[rows][columns];
//char arrow[rows][columns];

//Converts the list of words to a 2D array table
for(i=0; i<rows; i++){
    for(j=0; j<columns; j++){
        if (k<=MAXWORDS){
            words[i][j]=wordlist[k];
            k++;
        }

    }
}

for(i=0; i<rows; i++){
    for(j=0; j<columns; j++){
        if (k<=wordCount){
            printf("%15s", words[i][j]);
            k++;
        }
    }
    printf("\n");
}

// initscr();
// refresh();

// printWords(words, wordCount, rows, columns);
// refresh();

// sleep(5);
// endwin();

//printf("\n%s", wordlist[0]);


}


// void printWords(char* words, int wordCount, int rows, int columns){
// int i, j, k;
// for(i=0; i<rows; i++){
    // for(j=0; j<columns; j++){
        // if (k<=wordCount){
            // printw("%15s", words[i][j]);
            // k++;
        // }
    // }
    // printw("\n");
// }
// }    

void trimws(char* s) {
int len = strlen(s) ;
int x;
if (len ==0) return;
x = len-1;
while (isspace(s[x]) && (x>=0)) {
    s[x] = '\0';
    x--;
}
}

int readWords(char* wl[MAXWORDS], char* filename) {
int numread =0;
char line[WORDLEN];
char *p;
FILE* fp = fopen(filename,"r");
while (!feof(fp)) {
    p  =fgets(line, WORDLEN, fp);
    if (!feof(fp) && p !=NULL) {
        trimws(line);
        wl[numread] = (char *)  malloc(strlen(line)+1);
        strcpy(wl[numread], line);
        numread++;
        }
    } 
fclose(fp);
return numread; 
}

Code: http://pastebin.com/02A8nxEq Text File: http://pastebin.com/xk0MmkEm

Output:

$ ./lab10-3 wordslist.txt
16

Upvotes: 0

Views: 261

Answers (1)

Crowman
Crowman

Reputation: 25936

Just before you print your 2D array out here:

for(i=0; i<rows; i++){
    for(j=0; j<columns; j++){
        if (k<=wordCount){
            printf("%15s", words[i][j]);
            k++;
        }
    }
    printf("\n");
}

you need to reset k to 0, like so:

k = 0;
for(i=0; i<rows; i++){
    for(j=0; j<columns; j++){
        if (k<=wordCount){
            printf("%15s", words[i][j]);
            k++;
        }
    }
    printf("\n");
}

otherwise your if condition will always evaluate false, and you'll never print anything, since the previous loop already used it to count how many words you have.

Upvotes: 1

Related Questions