是数组的名称是否值? [英] is the name of an array an rvalue?
问题描述
可能重复:
数组名是C中的指针吗?
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;
}
我认为它不工作的原因是因为没有变量 a实际存储char *值。所以应该'a'被认为是一个右值?我不确定我是否正确理解了这个概念
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
推荐答案
数组不是指针, [Note 1:] 当数组名称无效时,数组的名称会衰减到指针。(例如:传递到函数)。
数组是不可修改的l值,无法分配,并且可以使用地址。
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.
例如:
在 sizeof()中使用数组名称不会衰减为指针 code>
无法更改数组地址,但可以更改内容。
Array address cannot be changed but content can be changed.
这篇关于是数组的名称是否值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!