Unix网络编程/socket编程

Unix network programming /socket programming

我收到以下错误

函数中w_Endline:

/home/prog2/in_out.c:113:19: error: assignment of read-only l       ocation ‘*(sent + (sizetype)(endlen * 1ul))’
       sent[endlen]='[=10=]';


/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]='[=11=]';
}
void w_White(const char* sent)
{
  size_t whitelen=strlen(sent);
  char* endlen=sent+whitelen;
  while(endlen>sent &&isspace(*endlen))
  {
    endlen='[=11=]';
    --endlen;
  }
}

2.in_out.h http://ideone.com/lDxxhY

如果您正在修改指针,请不要在 w_Endline 中使用 const,您还需要 ctype.h header 用于 isspace() 函数。同样在 w_White 函数中,如果您要分配一个 const 指针,则该指针也需要是 const。

 char* endlen=sent+whitelen;

应该是

const char* endlen=sent+whitelen;// because sent is const 

实际上const限定符的意思是read-only