• 3297阅读
  • 1回复

Question6.6:If you can't assign to arrays,then how can this work? [复制链接]

上一主题 下一主题
离线那个谁
 

只看楼主 倒序阅读 使用道具 楼主  发表于: 2013-07-09
— 本帖被 tyrone2497谁 执行加亮操作(2013-07-16) —

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.
离线那个谁

只看该作者 沙发  发表于: 2013-07-09
问题:如果不能对一个数组赋值,那么下面为什么是可以的?
      int f(char str[])
                       {
                                   if(str[0] == '\0')
                                         str = "none";...
                       }
回答:在这个代码中,str是一个函数的参数,因此它的声明也会像在问题6.4中所描述的那样被重新写入。换句话说,str是一个指针(char *类型的),并且向它赋值是合法的。
快速回复
限100 字节
如果您在写长篇帖子又不马上发表,建议存为草稿
 
上一个 下一个