免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1841 | 回复: 6
打印 上一主题 下一主题

关于const char的用法 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-06-09 11:42 |只看该作者 |倒序浏览
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.
我觉得象上面那样使用应该没有什么问题,但是编译不能通过

论坛徽章:
0
2 [报告]
发表于 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

论坛徽章:
0
3 [报告]
发表于 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
复制代码

论坛徽章:
0
4 [报告]
发表于 2003-06-09 13:13 |只看该作者

关于const char的用法

我用的是ATX 4.3

论坛徽章:
0
5 [报告]
发表于 2003-06-09 13:14 |只看该作者

关于const char的用法

不会吧,应该是line2 有问题,因为定义了const,是不可修改s的内容的!
我在sco unix下编译提示
error: left operand must be modifiable lvalue: op "="

论坛徽章:
0
6 [报告]
发表于 2003-06-09 13:21 |只看该作者

关于const char的用法

问题解决了,谢谢

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
7 [报告]
发表于 2003-06-09 13:23 |只看该作者

关于const char的用法

我编译提示

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

看来有些不仅仅是警告
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP