gpt4 book ai didi

字符指针数组

转载 作者:行者123 更新时间:2023-11-30 15:34:19 26 4
gpt4 key购买 nike

我得到了一个骨架代码,它为我提供了一个字符指针数组(wordslist),其中包含从 txt 文件读取的单词列表。我尝试将值移动到二维数组(单词)。它编译得很好,但是当我尝试打印数组时,我只是得到新行而不是字符串。

我认为问题可能与指针有关,特别是在第 55 行,但我不确定我做错了什么。

// 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;
}

代码:http://pastebin.com/02A8nxEq文本文件:http://pastebin.com/xk0MmkEm

输出:

$ ./lab10-3 wordslist.txt
16

最佳答案

在打印二维数组之前:

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

您需要将 k 重置为 0,如下所示:

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");
}

否则,您的 if 条件将始终计算为 false,并且您将永远不会打印任何内容,因为前一个循环已经使用它来计算您有多少个单词。

关于字符指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23353687/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com