Chinaunix

标题: 关于const char的用法 [打印本页]

作者: icw_zy    时间: 2003-06-09 11:42
标题: 关于const char的用法
void example(const char *s)
{
    if( s[0] != '0' )    // line 1
    {
        s[0] = '0';      // line 2
    }   
}
请问这样使用会不会出现问题
对于line 1,我编译程序的时候系统提示我:
An expression of type "const char" cannot be modified.
我觉得象上面那样使用应该没有什么问题,但是编译不能通过
作者: HopeCao    时间: 2003-06-09 11:59
标题: 关于const char的用法
http://chinaunix.net/forum/viewtopic.php?t=28245&highlight=const

http://www.chinaunix.net/forum/viewtopic.php?t=76915
作者: HopeCao    时间: 2003-06-09 12:03
标题: 关于const char的用法
你用的是什么系统?
我在Linux下:

  1. #include <stdlib.h>;
  2. #include <stdio.h>;

  3. void example(const char *s)
  4. {
  5.         if( s[0] != '0' ) // line 1
  6.         {
  7.                 s[0] = '0'; // line 2
  8.         }
  9. }

  10. int main(void)
  11. {
  12.         example("aaaa");
  13.         return 1;
  14. }
复制代码


  1. cc test.c
  2. test.c: In function `example':
  3. test.c:8: warning: assignment of read-only location
复制代码

作者: icw_zy    时间: 2003-06-09 13:13
标题: 关于const char的用法
我用的是ATX 4.3
作者: li2002    时间: 2003-06-09 13:14
标题: 关于const char的用法
不会吧,应该是line2 有问题,因为定义了const,是不可修改s的内容的!
我在sco unix下编译提示
error: left operand must be modifiable lvalue: op "="
作者: icw_zy    时间: 2003-06-09 13:21
标题: 关于const char的用法
问题解决了,谢谢
作者: 蓝色键盘    时间: 2003-06-09 13:23
标题: 关于const char的用法
我编译提示

cc: "x.c", line 8: error 1549: Modifiable lvalue required for assignment operator.

看来有些不仅仅是警告




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2