这段代码的作用是跳过空格并逐个返回单词。对于这段代码,有几个问题:当代码到达*word++=c;行时,我会得到一个核心转储。我是否正确编写了此行?返回是否正确?我需要分配内存来存储单词吗?
//get_word
int get_word(char *word,int lim){
int i=0;
int c;
int quotes=0;
int inword = 1;
while(
inword &&
(i < (lim-1)) &&
((c=getchar()) != EOF)
){
if(c==('\"')){//this is so i can get a "string"
if (quotes) {
inword = 0;
}
quotes = ! quotes;
}
else if(quotes){ //if in a string keep storing til the end of the string
*word++=c;//pointer word gets c and increments the pointer
i++;
}
else if(!isspace(c)) {//if not in string store
*word++=c;
i++;
}
else {
// Only end if we have read some character ...
if (i)
inword = 0;
}
}
*word='\0'; //null at the end to signify
return i; //value
}
malloc
的返回值中删除强制转换吗? - R.. GitHub STOP HELPING ICEmalloc
返回的是void*
:错误C2440:“初始化”:无法从“void”转换为“char” 从'void*'到非'void'指针的转换需要显式转换。 - Steve Townsendmalloc
的 MSDN CRT 文档说需要进行类型转换:"要返回指向除 void 以外的其他类型的指针,请在返回值上使用类型转换。" - Steve Townsend