简体   繁体   English

行 *x=y 是否等于 c 中的行 x[0]=y

[英]is the line *x=y equal to the line x[0]=y in c

in c, when allocating a new dynamic memory with malloc:在 c 中,当使用 malloc 分配新的动态 memory 时:

int* x = (int*)malloc(sizeof(x));
int y = 10;

is this line:这条线是:

*x = y;

equal to this line:等于这一行:

x[0] = y;

The definition of the [] operator is: given ex1[ex2] , it is guaranteed to be equivalent to []运算符的定义是:给定ex1[ex2] ,保证等价于

*((ex1) + (ex2))

Where ex1 and ex2 are expressions.其中ex1ex2是表达式。

In your case x[0] == *(x + 0) == *(x) == *x .在您的情况下x[0] == *(x + 0) == *(x) == *x

See Do pointers support "array style indexing"?请参阅指针是否支持“数组样式索引”? for details.详情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM