C - 代码"[ccls] 数组类型'char [40]'不可分配"的问题



我正在编写一个使用结构来工作的代码,该代码应该从一个文件中获取句子,并将它们组织在不同的文件中。一切都正常工作,但后来我被告知我做结构的方式是错误的,而不是做

typedef struct s_words {
char * str; //word
int count; //number of times word occurs
struct s_words * next; //pointer to next word
}words;

我被告知要把它改成

typedef struct s_word {
char word[40];
int count;
struct s_word* next;
} words;

当我这样做的时候,出现了很多问题,尽管我相信我几乎已经完成了,但所有的问题都是我无法真正解决的。问题是";[clls]数组类型'char[40]'不可赋值"在以下部分

while (temp -> next != NULL) {
if (strcmp(temp -> word, temp -> next -> word) > 0) {
printf("|cmp1: %s t", temp -> word);
printf("cmp2: %s t", temp -> next -> word);
printf("cmp: %d t", strcmp(temp -> word, temp -> next -> word));
swapTemp = temp -> word;
temp -> word = temp -> next -> word;
temp -> next -> word = swapTemp;
printf("next word: %s| t", temp -> next -> word);
temp = * wordList;
}
// else {
printf("%s ", temp -> word);
// printf("next word: %s t", temp->next->str);
temp = temp -> next;
// }
}

以下2个

temp -> word = temp -> next -> word;
temp -> next -> word = swapTemp;

有问题

完整的代码在下面


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// structure defanition of a node
typedef struct s_word {
char word[40];
int count;
struct s_word *next;
} words;
// declaring head and tail pointers of the stopword list
words*stopwordHead = NULL, *stopwordLast = NULL;
words * create_words(char * word); 
// method to add the stopwords read from the file to a list
void add_stopWord()
{  
char buff[255]; //creating char array to store data of file  
FILE *myFile = fopen("stopwords.txt", "r"); // open the stopwords.txt file
// check for errors while file opening
if (myFile == 0) 
{
printf("Stopword file not openedn");
exit(0);
} 
words *temp = stopwordHead;
// read each word from the file
while (fscanf(myFile, "%s",buff) != EOF)
{   
// and add it to the list
if (stopwordHead ==  NULL) 
{
stopwordHead = stopwordLast =  create_words(buff);

}
// create new node with current word as its value
temp = create_words(buff);
// add new node to tail of the list and update the tail pointer of the list
stopwordLast->next = temp;
stopwordLast = temp;    
}
fclose(myFile); // close the file
}
words* add_word(words ** wordList, char * word) {
words *tem  = stopwordHead;
int flag = 0;

// check wheter  current word read from text.txt is a stopword or not
// if yes, then set flag = 1
while(tem != NULL)
{
if(strcmp(word, tem->word) == 0)
{
flag = 1;
break;
}
tem = tem->next;
}
// if flag == 1, thenn don't add the word to the wordList
if(flag)
return *wordList;
if (! *wordList) {
/* handle EMPTY list */
printf("NEW LISTn");
return *wordList = create_words(word);
}

words *temp = *wordList;
//+ search if word exists in the list; if so, make found=1
while (temp -> next != NULL) {
/* iterate while temp->next != NULL */
if (strcmp(temp -> word, word) == 0) { //+use strcmp command
temp -> count = temp -> count + 1; //+increment count;
return *wordList;
} else
temp = temp -> next; //+update temp
}
words * newWord = create_words(word);
if (NULL != newWord) {
/* insert at TAIL of list */
temp -> next = newWord;
printf(" NEW WORD: %sn ", newWord -> word);
temp = * wordList;
char * swapTemp;
while (temp -> next != NULL) {
if (strcmp(temp -> word, temp -> next -> word) > 0) {
printf("|cmp1: %s t", temp -> word);
printf("cmp2: %s t", temp -> next -> word);
printf("cmp: %d t", strcmp(temp -> word, temp -> next -> word));
swapTemp = temp -> word;
temp -> word = temp -> next -> word;
temp -> next -> word = swapTemp;
printf("next word: %s| t", temp -> next -> word);
temp = * wordList;
}
// else {
printf("%s ", temp -> word);
// printf("next word: %s t", temp->next->str);
temp = temp -> next;
// }
}
printf("next word: %s t", temp -> word);
}
return newWord;
}
int concordance() {
words * mywords; //+head of linked list containing words
// call the method to create a list with stopwords
mywords = NULL;
char * delim = ". ,:;tn";
int counter = 0;
FILE * myFile;
FILE * myOutput;
myFile = fopen("text.txt", "r"); //+first parameter is input file
if (myFile == 0) {
printf("file not openedn");
return 1;
} else {
printf("file opened n");
}
//+start reading file character by character;
//+when word has been detected; call the add_word function
int ch = 0, word = 1, k = 0;
char thisword[100];
while ((ch = fgetc(myFile)) != EOF) {
/* for each char */
if (strchr(delim, ch)) {
/* check if delim */
if (word == 1) {
/* if so, terminate word, reset */
word = 0;
thisword[k] = '';
printf("nadd_word (mywords, %s)n", thisword);
/* do NOT overwrite list address each time,
* you must send ADDRESS of list to add_word
* to handle EMPTY list case.
*/
if (add_word( &mywords, thisword))
printf(" added: %sn", mywords -> word);
else
fprintf(stderr, "error: add_word failed.n");
k = 0;
}
} else {
/* if not delim, add char to string, set word 1 */
word = 1;
thisword[k++] = tolower(ch); /* make ch lowercase */
}
}
if (word == 1) {
/* handle non-POSIX line-end */
thisword[k] = '';
//add thisword into the list
printf("nadd_word (mywords, %s) (last)n", thisword);
if (add_word( & mywords, thisword)) /* same comment as above */
printf(" added: %sn", mywords -> word);
else
fprintf(stderr, "error: add_word failed.n");
}
words * currword;
printf("printing listn");
//+Traverse list and print each word and its count to outputfile
//+output file is second parameter being passed
myOutput = fopen("concordance.txt", "w+"); //+first parameter is input file
if (myOutput == 0) {
printf("output file not opened n");
return 1;
} else {
printf("output file opened n");
}
currword = mywords;
while (currword != NULL) {
/* just test currword here */
currword = currword -> next;
counter++;
}
fprintf(myOutput, "There are %d distinct words in the text file:n", counter);
currword = mywords;
while (currword != NULL) {
/* just test currword here */
//add word name then word count to file, then move to next
fprintf(myOutput, "%s %d n", currword -> word, currword -> count);
printf("%s ", currword -> word);
currword = currword -> next;
}
putchar('n');
return 1;
}
int stopwords() {
words * mywords; //+head of linked list containing words
add_stopWord();
// call the method to create a list with stopwords
mywords = NULL;
char * delim = ". ,:;tn";
int counter = 0;
FILE * myFile;
FILE * myOutput;
myFile = fopen("text.txt", "r"); //+first parameter is input file
if (myFile == 0) {
printf("file not openedn");
return 1;
} else {
printf("file opened n");
}
//+start reading file character by character;
//+when word has been detected; call the add_word function
int ch = 0, word = 1, k = 0;
char thisword[100];
while ((ch = fgetc(myFile)) != EOF) {
/* for each char */
if (strchr(delim, ch)) {
/* check if delim */
if (word == 1) {
/* if so, terminate word, reset */
word = 0;
thisword[k] = '';
printf("nadd_word (mywords, %s)n", thisword);
/* do NOT overwrite list address each time,
* you must send ADDRESS of list to add_word
* to handle EMPTY list case.
*/
if (add_word( &mywords, thisword))
printf(" added: %sn", mywords -> word);
else
fprintf(stderr, "error: add_word failed.n");
k = 0;
}
} else {
/* if not delim, add char to string, set word 1 */
word = 1;
thisword[k++] = tolower(ch); /* make ch lowercase */
}
}
if (word == 1) {
/* handle non-POSIX line-end */
thisword[k] = '';
//add thisword into the list
printf("nadd_word (mywords, %s) (last)n", thisword);
if (add_word( & mywords, thisword)) /* same comment as above */
printf(" added: %sn", mywords -> word);
else
fprintf(stderr, "error: add_word failed.n");
}
words * currword;
printf("printing listn");
//+Traverse list and print each word and its count to outputfile
//+output file is second parameter being passed
myOutput = fopen("concordance_wo_stop_words.txt", "w+"); //+first parameter is input file
if (myOutput == 0) {
printf("output file not opened n");
return 1;
} else {
printf("output file opened n");
}
currword = mywords;
while (currword != NULL) {
/* just test currword here */
currword = currword -> next;
counter++;
}
fprintf(myOutput, "There are %d distinct words in the text file:n", counter);
currword = mywords;
while (currword != NULL) {
/* just test currword here */
//add word name then word count to file, then move to next
fprintf(myOutput, "%s %d n", currword -> word, currword -> count);
printf("%s ", currword -> word);
currword = currword -> next;
}
putchar('n');
return 1;
}
int main() {
concordance();
stopwords();
return 0;
}

任何帮助都将不胜感激。

数组没有赋值运算符。

例如,代替此代码片段的

swapTemp = temp -> word;
temp -> word = temp -> next -> word;
temp -> next -> word = swapTemp;

你需要写

char swapTemp[ sizeof( temp->word ) ];
strcpy( swapTemp, temp->word );
strcpy( temp->word, temp->next->word );
strcpy( temp->next->word, swapTemp );  

我没有看过你所有的代码,但例如这个代码片段

if (stopwordHead ==  NULL) 
{
stopwordHead = stopwordLast =  create_words(buff);

}
// create new node with current word as its value
temp = create_words(buff);
// add new node to tail of the list and update the tail pointer of the list
stopwordLast->next = temp;
stopwordLast = temp;    

是错误的,并且由于在表达式CCD_ 2中使用了空指针CCD_。并且最初对于存储在阵列buff中的相同字符串调用create_words两次。

最新更新