在C多维数组指针地址

编程入门 行业动态 更新时间:2024-10-28 21:24:08
本文介绍了在C多维数组指针地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我瞎搞多维数组和指针。我一直在寻找打印出的内容,以及地址,一个简单的数组的程序。这里是我的数组声明:

I'm messing around with multidimensional arrays and pointers. I've been looking at a program that prints out the contents of, and addresses of, a simple array. Here's my array declaration:

int zippo[4][2] = { {2,4}, {6,8}, {1,3}, {5,7} };

我目前的理解是, ZIPPO 是一个指针,它可以容纳几个其他指针的地址。默认情况下, ZIPPO 持有指针的zippo [0] ,它也可以拥有指针的地址的地址 ZIPPO [1] , ZIPPO [2] 和 ZIPPO [3] 。

My current understanding is that zippo is a pointer, and it can hold the address of a couple of other pointers. By default, zippo holds the address of pointer zippo[0], and it can also hold the addresses of pointers zippo[1], zippo[2], and zippo[3].

现在,采取以下语句:

printf("zippo[0] = %p\n", zippo[0]); printf(" *zippo = %p\n", *zippo); printf(" zippo = %p\n", zippo);

在我的机器上,给出了以下的输出:

On my machine, that gives the following output:

zippo[0] = 0x7fff170e2230 *zippo = 0x7fff170e2230 zippo = 0x7fff170e2230

我完全理解为什么的zippo [0] 和 * ZIPPO 有相同的值。他们都是三分球,他们都存储整数2的地址(默认),或的zippo [0] [0] 。但随着 ZIPPO 也共享相同的存储地址是什么呢?不应该 ZIPPO 来存储指针的地址的zippo [0] ? Whaaaat?

I perfectly understand why zippo[0] and *zippo have the same value. They're both pointers, and they both store the address (by default) of the integer 2, or zippo[0][0]. But what is up with zippo also sharing the same memory address? Shouldn't zippo be storing the address of the pointer zippo[0]? Whaaaat?

推荐答案

在声明多维数组时,编译器将其视为一个单一的维数组。多维数组只是一个抽象,让我们的生活更轻松。你有一个误区:这不是一个阵列指向4阵列,它始终只是一个单一的contigous的内存块

When you declare a multidimensional array, the compiler treats it as a single dimensional array. Multidimensional arrays are just an abstraction to make our life easier. You have a misunderstanding: This isn't one array pointing to 4 arrays, its always just a single contigous block of memory.

在你的情况,这样做的:

In your case, doing:

int zippo[4][2]

真是一样做

int zippo[8]

随着二维所需的数学寻址编译器为您处理。

With the math required for the 2D addressing handled for you by the compiler.

有关详细信息,请参阅本阵列教程C ++ 。

For details, see this tutorial on Arrays in C++.

这是非常不同的比做:

int** zippo

int* zippo[4]

在这种情况下,你正在做的四个指针数组,可以任意分配给其他数组。

In this case, you're making an array of four pointers, which could be allocated to other arrays.

更多推荐

在C多维数组指针地址

本文发布于:2023-07-30 11:03:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1248820.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多维   数组   指针   地址

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!