使用多个元素访问指针与访问指针?
嗨,我是C的新手,正在试图了解指针和阵列之间的细微差别。具体而言,我不明白什么从根本上区分以下两个对象:
int *obj1[N];
int *obj2;
obj2 = malloc(N*sizeof(int))
这两个对象本质上为我提供了一系列指针,但我会以不同的方式引用它们以检索它们所指向的价值:
val1 = *obj1[i];
val2 = obj2[i];
我认为这两个都在最终效果“指针的阵列”中, (尽管我理解对OBJ2的“数组”一词在技术上不是正确的),但是OBJ2似乎隐含地认识到我想要该值而无需取消 *而OBJ1不需要。有人可以为我澄清这两个对象之间导致上述参考价值的差异之间的结构差异吗?
Hi I am new to C and am trying to understand the nuance between pointers and arrays. Specifically I don't understand what fundamentally distinguishes the following two objects:
int *obj1[N];
int *obj2;
obj2 = malloc(N*sizeof(int))
Both objects provide me essentially an array of pointers but I would reference them differently to retrieve the values they point to:
val1 = *obj1[i];
val2 = obj2[i];
I consider both of them in end effect 'arrays of pointers' (though I understand the use of the word 'array' for the obj2 is not technically correct), however obj2 seems to implicitly recognize that I want the value without the need to dereference with * while obj1 doesn't. Could someone clarify for me the structural difference between these two objects which leads to the above difference in referencing their values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该声明
确实声明了指针类型
int *
的对象数组。该声明
声明了类型
int *
的对象,在下一个语句中,分配了内存,用于
int
的n个元素的数组。因此,在此行中
一样重写
,为了清楚起见,最好像您获得类型
int
的对象 。在第一行中,表达式obj1 [i]
产生一个类型int *
的对象,因为obj1
是指针的数组。因此,要获取类型int
的尖头对象,您需要取消指针表达式obj1 [i]
。在第二行中,表达式obj2 [i]
产生了类型int
的对象,因为分配了整数数组。如果要分配类型
int *
的指针
数组i] 数组设计师
obj1
被隐式转换为类型int **
的指针到其第一个元素。因此,两种表达式obj1 [i]
和obj2 [i]
是等效评估的。另外,两种表达式*obj1 [i]
和*obj2 [i]
等效于obj1 [i] [0] [0]
和OBJ2 [i] [0]
。另一方面,您还可以记住,
请记住,根据C标准(6.5.2.1阵列订阅)
This declaration
indeed declares an array of objects of the pointer type
int *
.This declaration
declares an object of the type
int *
and in the next statementthere is allocated memory for an array of N elements of the type
int
.So in this lines
that for clarity are better to rewrite like
you get objects of the type
int
. In the first line the expressionobj1[i]
yields an object of the typeint *
becauseobj1
is an array of pointer. So to get the pointed object of the typeint
you need to dereference the pointer expressionobj1[i]
. In the second line the expressionobj2[i]
yields an object of the typeint
because there was allocated an array of integers.If you want to allocate an array of pointers of the type
int *
you have to writeIn this case to access elements of the both arrays you can write
Pay attention to that in this expression
obj1[i]
the array designatorobj1
is implicitly converted to a pointer of the typeint **
to its first element. So the both expressionsobj1[i]
andobj2[i]
are equivalently evaluated. Also the both expressions*obj1[i]
and*obj2[i]
are equivalent toobj1[i][0]
andobj2[i][0]
.On the other hand, you could also write for example
Bear in mind that according to the C Standard (6.5.2.1 Array subscripting)
第一个是
int*
的数组,指向int
,据说是本地分配的(在堆栈上)。他们尚未指出任何有效的数据。第二个是
int
动态分配的数组(在堆上)。它尚未包含任何有效的值。,请参见上文。
,因为它们是用于不同目的的不同数据类型。
,这是错误的。
要动态分配类似于
int *obj1 [n];
的指针数组,您必须这样做:The first is an array of
int*
, pointers toint
, supposedly allocated locally (on the stack). They do not point at any valid data yet.The second is an array of
int
allocated dynamically (on the heap). It does not contain any valid values yet.No, see above.
Yeah you will have to since they are different data types used for different purposes.
Again, this is wrong.
To dynamically allocate an array of pointers similar to
int *obj1[N];
, you have to do this: