我收到以下错误
在功能w_Endline
中:
/home/prog2/in_out.c:113:19: error: assignment of read-only l ocation ‘*(sent + (sizetype)(endlen * 1ul))’
sent[endlen]='\0';
/home/prog2/in_out.c: In function ‘w_White’:
/home/prog2/in_out.c:119:19: warning: initialization discards ‘const’ qualifier from pointer target type
char* endlen=sent+whitelen;
/home/prog2/in_out.c:120:6: warning: implicit declaration of function ‘isspace’ [-Wimplicit-function-declaration]
while(endlen>sent &&isspace(*endlen))
文件
1.in_out.c http://ideone.com/nI15F4
void w_Endline(const char* sent)
{
size_t endlen=strlen(sent)-1;
if(sent[endlen]=='\n')
sent[endlen]='\0';
}
void w_White(const char* sent)
{
size_t whitelen=strlen(sent);
char* endlen=sent+whitelen;
while(endlen>sent &&isspace(*endlen))
{
endlen='\0';
--endlen;
}
}
2.in_out.h http://ideone.com/lDxxhY
答案 0 :(得分:0)
如果要修改指针,请不要在w_Endline
中使用const,ctype.h
函数需要isspace()
标头。同样在w_White
函数中,如果要分配const指针,则指针也需要为const。
char* endlen=sent+whitelen;
应该是
const char* endlen=sent+whitelen;// because sent is const
实际上const限定符意味着只读