使用多个元素访问指针与访问指针?

发布于 2025-02-06 12:31:06 字数 377 浏览 4 评论 0原文

嗨,我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红尘作伴 2025-02-13 12:31:06

该声明

int *obj1[N];

确实声明了指针类型int *的对象数组。

该声明

int *obj2;

声明了类型int *的对象,在下一个语句中,

obj2 = malloc(N*sizeof(int));

分配了内存,用于int的n个元素的数组。

因此,在此行中

val1 = *obj1[i];
val2 = obj2[i];

一样重写

int val1 = *obj1[i];
int val2 = obj2[i];

,为了清楚起见,最好像您获得类型int的对象 。在第一行中,表达式obj1 [i]产生一个类型int *的对象,因为obj1是指针的数组。因此,要获取类型int的尖头对象,您需要取消指针表达式obj1 [i]。在第二行中,表达式obj2 [i]产生了类型int的对象,因为分配了整数数组。

如果要分配类型int *

int **obj2 = malloc(N*sizeof(int *));

指针

int val1 = *obj1[i];
int val2 = *obj2[i];

数组i] 数组设计师obj1被隐式转换为类型int **的指针到其第一个元素。因此,两种表达式obj1 [i]obj2 [i]是等效评估的。另外,两种表达式*obj1 [i]*obj2 [i]等效于obj1 [i] [0] [0]OBJ2 [i] [0]

另一方面,您还可以记住,

int **obj3 = obj1;  // the array is implicitly converted to pointer
//...
int val3 = *obj3[i];

请记住,根据C标准(6.5.2.1阵列订阅)

2一个后缀表达式,然后在方括号中表达式[]
是数组对象元素的订阅名称。这
下标运算符的定义是E1 [e2]与
(*((E1)+(E2)))。由于适用于
二进制 +运算符,如果E1是一个数组对象(等效地,指针
对于数组对象的初始元素)和E2是整数,
E1 [E2]指定E1的E2元素(从零计数)

This declaration

int *obj1[N];

indeed declares an array of objects of the pointer type int *.

This declaration

int *obj2;

declares an object of the type int * and in the next statement

obj2 = malloc(N*sizeof(int));

there is allocated memory for an array of N elements of the type int.

So in this lines

val1 = *obj1[i];
val2 = obj2[i];

that for clarity are better to rewrite like

int val1 = *obj1[i];
int val2 = obj2[i];

you get objects of the type int. In the first line the expression obj1[i] yields an object of the type int * because obj1 is an array of pointer. So to get the pointed object of the type int you need to dereference the pointer expression obj1[i]. In the second line the expression obj2[i] yields an object of the type int because there was allocated an array of integers.

If you want to allocate an array of pointers of the type int * you have to write

int **obj2 = malloc(N*sizeof(int *));

In this case to access elements of the both arrays you can write

int val1 = *obj1[i];
int val2 = *obj2[i];

Pay attention to that in this expression obj1[i] the array designator obj1 is implicitly converted to a pointer of the type int ** to its first element. So the both expressions obj1[i] and obj2[i] are equivalently evaluated. Also the both expressions *obj1[i] and *obj2[i] are equivalent to obj1[i][0] and obj2[i][0].

On the other hand, you could also write for example

int **obj3 = obj1;  // the array is implicitly converted to pointer
//...
int val3 = *obj3[i];

Bear in mind that according to the C Standard (6.5.2.1 Array subscripting)

2 A postfix expression followed by an expression in square brackets []
is a subscripted designation of an element of an array object. The
definition of the subscript operator [] is that E1[E2] is identical to
(*((E1)+(E2))). Because of the conversion rules that apply to the
binary + operator, if E1 is an array object (equivalently, a pointer
to the initial element of an array object) and E2 is an integer,
E1[E2] designates the E2-th element of E1 (counting from zero)

孤星 2025-02-13 12:31:06

第一个是int*的数组,指向int,据说是本地分配的(在堆栈上)。他们尚未指出任何有效的数据。

第二个是int动态分配的数组(在堆上)。它尚未包含任何有效的值。

两个对象本质上为我提供了一系列指针

,请参见上文。

,但我会以不同的方式引用它们

,因为它们是用于不同目的的不同数据类型。

我认为它们两个都在“指针阵列”

的最终效果中

,这是错误的。

要动态分配类似于int *obj1 [n];的指针数组,您必须这样做:

int** ptrptr = malloc(n * sizeof *ptr);

The first is an array of int*, pointers to int, 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.

Both objects provide me essentially an array of pointers

No, see above.

but I would reference them differently

Yeah you will have to since they are different data types used for different purposes.

I consider both of them in end effect 'arrays of pointers'

Again, this is wrong.

To dynamically allocate an array of pointers similar to int *obj1[N];, you have to do this:

int** ptrptr = malloc(n * sizeof *ptr);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文