我瞎搞多维数组和指针。我一直在寻找打印出的内容,以及地址,一个简单的数组的程序。这里是我的数组声明:
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多维数组指针地址
发布评论