如何处理数组指针?

发布于 2024-12-03 02:24:34 字数 472 浏览 3 评论 0原文

该程序正确打印所有数组输出。但这个程序到底是如何运作的呢?为什么这里需要s[i]的地址?

#include <stdio.h>

int main(){
        int s[4][2] = {{1234,1},{1233,2},{1232,3},{1331,4}};
        int (*p)[2];
        int i,j,*pint;

        for(i=0;i<4;++i){
                p = &s[i];
                pint = (int*)p;
                printf("\n");
                for(j=0;j<=1;++j)
                        printf("%d ",*(pint+j));
        }
        return 0;
}

This program prints all arrays outputs correctly. But how does this program work exactly? Why do we need address of s[i] here?

#include <stdio.h>

int main(){
        int s[4][2] = {{1234,1},{1233,2},{1232,3},{1331,4}};
        int (*p)[2];
        int i,j,*pint;

        for(i=0;i<4;++i){
                p = &s[i];
                pint = (int*)p;
                printf("\n");
                for(j=0;j<=1;++j)
                        printf("%d ",*(pint+j));
        }
        return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

抹茶夏天i‖ 2024-12-10 02:24:34

int (*p)[2] 是一个指向 int [2] 类型的指针,它本身本质上是一个 int * 类型,但具有对数组边界进行一些额外的类型检查。

因此,在 p = &s[i]; 中,您将 p 设置为指向内存中数组 s[i 所在区域的指针的地址] 是。

p 也成为一个数组(即,“本质上”是一个指向带有附加机制的内存区域的指针)要容易得多,然后直接使用它来指向数组内存区域(<代码>p = s[i])。然而,在这种情况下,这正是 pint (作为真正的指针)正在做的事情,所以我们可以完全删除 p

因此:

#include <stdio.h>

int main(){
        int s[4][1] = {{1234,1},{1233,2},{1232,3},{1331,4}};
        int i,j,*pint;

        for(i=0;i<4;++i){
                pint = (int*)s[i];
                printf("\n");
                for(j=0;j<=1;++j)
                        printf("%d ",*(pint+j));
        }
        return 0;
}

请参阅数组和指针或谷歌搜索“C 数组和指针”还有 C 多维中的指针地址数组

请注意,我假设您这样做只是为了玩弄并了解指针和数组如何交互,因此我不会评论在每种情况下使用指针算术或数组概念等是否理想。


另请注意,我说数组“本质上是一个指针 [...]”,但是,这并不完全正确,它在很多情况下只是充当指针,并且在大多数情况下,这是一种合理的方式想想事情是如何运作的。实际上,数组是以特殊方式处理的。请参阅 数组名称等同于指针吗?数组

int (*p)[2] is a pointer to an int [2] type, which itself is essentially a int * type but with some additional type checking on the array bounds.

So in p = &s[i]; you are setting p to be the address of the pointer to the area in memory where the array s[i] is.

It's much much easier to just make p also an array (i.e., "essentially" a pointer to an area in memory with additional machinery), and then use that directly to point at the array memory area (p = s[i]). However, in that case, that's exactly what pint (as a true pointer instead) is doing, so we can remove p entirely.

So:

#include <stdio.h>

int main(){
        int s[4][1] = {{1234,1},{1233,2},{1232,3},{1331,4}};
        int i,j,*pint;

        for(i=0;i<4;++i){
                pint = (int*)s[i];
                printf("\n");
                for(j=0;j<=1;++j)
                        printf("%d ",*(pint+j));
        }
        return 0;
}

See Arrays and pointers or google for "C arrays and pointers" and also Pointer address in a C multidimensional array.

Note, I assume you are just doing this to play around and understand how pointers and arrays interact, so I make no comment on whether it is ideal to use pointers arithmetic or array notion and so on in each case.


Note also, that I say an array is "essentially a pointer [...]", however, this is not strictly true, it just acts like a pointer in a lot of cases and for the most part this is a reasonable way to think of how things are working. In reality, arrays are treated in a special way. See Is array name equivalent to pointer? and Arrays.

隱形的亼 2024-12-10 02:24:34
#include <stdio.h>

int main(){
        int s[4][2] = {{1234,1},{1233,2},{1232,3},{1331,4}};
        int (*p)[2];
        int i,j,*pint;

        for(i=0;i<4;++i){
                p = &s[i];
                pint = (int*)p;
                printf("\n");
                for(j=0;j<=1;++j)
                        printf("%d ",*(pint+j));
        }
        return 0;
}

这里 p 被定义为一个数组,它可以存储两个整数值,

 int (*p)[2];

在外部 for 循环中我们使用 p 数组来存储 4 {1 维} 。一次 1 个。

 p = &s[i];

也就是说,在第一次迭代中,它将是

p = &s[0] ;
i.e p = {1234,1}

下一次迭代

 p = &s[1] ;
i.e p = {1234,2}

,依此类推。这就是为什么我们需要 &S[i]

内循环
只需迭代一维数组即可打印元素

#include <stdio.h>

int main(){
        int s[4][2] = {{1234,1},{1233,2},{1232,3},{1331,4}};
        int (*p)[2];
        int i,j,*pint;

        for(i=0;i<4;++i){
                p = &s[i];
                pint = (int*)p;
                printf("\n");
                for(j=0;j<=1;++j)
                        printf("%d ",*(pint+j));
        }
        return 0;
}

here p is defined as an array which can store two integer values

 int (*p)[2];

in the outer for loop we use p array to store the 4 {1 dimensional} . 1 at a time .

 p = &s[i];

that is in first iteration it will be

p = &s[0] ;
i.e p = {1234,1}

the next iteration will be

 p = &s[1] ;
i.e p = {1234,2}

and so on. that is why we need &S[i]

the inner loop
just iterate throug the 1 dimensional array to print the element

最舍不得你 2024-12-10 02:24:34

啊。我不确定这段代码除了如何使易于理解的代码难以理解之外还应该说明什么。

sint 2 元素数组的 4 元素数组。也就是说,对于 0..3 中的 is[i] 的类型是 int [2](在大多数情况下会衰减)为int *),&s[i]的类型为int (*)[2]

p 是一个指向 int 2 元素数组的指针。每次循环时,p 都会被分配 s[i] 处的 2 元素数组的地址。

最后,pint是一个指向int的简单指针,每次循环都被设置为指向p指向的数组中的第一个元素代码>. *(pint + j)pint[j] 的长写法。

请注意,ppint 都是完全多余的;这基本上是一种冗长的写作方式

for (i = 0; i < 4; i++)
{
  for (j = 0; j < 2; j++)
    printf("%d ", s[i][j]);
  printf("\n");
}

Ugh. I'm not sure what this code is supposed to illustrate other than how to make easy-to-understand code hard to understand.

s is a 4-element array of 2-element arrays of int. That is, for i in 0..3, the type of s[i] is int [2] (which in most contexts decays to int *), and the type of &s[i] is int (*)[2].

p is a pointer to a 2-element array of int. Each time through the loop, p is assigned the address of the 2-element array at s[i].

Finally, pint is a simple pointer to int, and each time through the loop is set to point to the first element in the array pointed to by p. *(pint + j) is the long way of writing pint[j].

Note that both p and pint are entirely superfluous; this is basically a long-winded way of writing

for (i = 0; i < 4; i++)
{
  for (j = 0; j < 2; j++)
    printf("%d ", s[i][j]);
  printf("\n");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文