【发布时间】:2010-10-23 20:00:39
【问题描述】:
此代码应该跳过空格并一次返回一个单词。关于这段代码的几个问题: 当代码到达 *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
}
【问题讨论】: