找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 238|回复: 1

Question6.6:If you can't assign to arrays,then how can this work?

[复制链接]

210

主题

371

回帖

0

积分

管理员

积分
0
发表于 2013-7-9 21:21:46 | 显示全部楼层 |阅读模式

Q: If you can't assign to arrays, then how can
                       int f(char str[])
                       {
                                   if(str[0] == '\0')
                                         str = "none";...
                       }work?

A: In this code, str is a function parameter, so its declaration is rewritten by the compiler as described in question 6.4. In other words, str is a pointer (of type char *), and it is legal to assign to it.

210

主题

371

回帖

0

积分

管理员

积分
0
 楼主| 发表于 2013-7-9 21:25:10 | 显示全部楼层
问题:如果不能对一个数组赋值,那么下面为什么是可以的?
      int f(char str[])
                       {
                                   if(str[0] == '\0')
                                         str = "none";...
                       }
回答:在这个代码中,str是一个函数的参数,因此它的声明也会像在问题6.4中所描述的那样被重新写入。换句话说,str是一个指针(char *类型的),并且向它赋值是合法的。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

果子博客
扫码关注微信公众号

Archiver|手机版|小黑屋|风叶林

GMT+8, 2026-2-1 04:38 , Processed in 0.082307 second(s), 20 queries .

Powered by 风叶林

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表