是一个数组右值的名字吗? [英] is the name of an array an rvalue?

查看:90
本文介绍了是一个数组右值的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  是数组名在C中的指针?


 的#include<&stdlib.h中GT;
INT主(INT ARGC,为const char * argv的[])
{
    字符* B =(字符*)malloc的(的sizeof(char)的* 50);
    B =(字符*)的Hello World;
    //作品    所以char a [50];
    A =(字符*)的Hello World;
    //不起作用。为什么?我以为数组名是公正的指针点
    //到所述阵列的所述第一元件(这是CHAR)。所以不是一个char *?
    返回0;
}

我觉得它不工作的原因是因为没有所谓的变量a,实际上存储一个char *值。所以要'一'被认为是一个右值?我不知道如果我正确地理解概念


解决方案

数组不是指针,有时 [注1:] 的数组的名称衰变到一个指针数组时名称是无效(的例如:传递给函数的)。结果
阵列的不可修改左值,它们不能被分配​​的,有地址,可以采取。

[注1:] 结果
例如:结果
使用时,数组名不衰减的指针的sizeof()

阵地址不能被改变,但内容可以改变。

Possible Duplicate:
Is array name a pointer in C?

#include <stdlib.h>
int main(int argc, const char *argv[])
{
    char *b=(char*)malloc(sizeof(char)*50);
    b=(char*)"hello world";
    // works

    char a[50];
    a=(char*)"hello world";
    //doesn't work. why? I thought array names are just pointers that point
    //to the first element of the array (which is char). so isn't a char*?
    return 0;
}

I think the reason it doesn't work is because there's no variable called "a" that actually stores a char* value. so should 'a' be considered an rvalue? I'm not sure if I'm understanding the concept correctly

解决方案

Arrays are not pointers, sometimes[Note 1:] the name of an array decays to a pointer when array name is not valid(eg: passing to function).
Arrays are non modifiable l-values, they cannot be assigned and there address can be taken.

[Note 1:]
For example:
Array name doesn't decay to a pointer when used in sizeof()

Array address cannot be changed but content can be changed.

这篇关于是一个数组右值的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆