简体   繁体   English

指向2D数组的指针

[英]Pointer to 2D Array

I'm given the following snippet of code: 我得到了以下代码片段:

int group1[3][3] = {3,4,5,1,9,8};
int group2[3][3] = {{1},{2,3},{4,5}};
int *gPtr1 = group1;
int *gPtr2 = group2;

The question asks me for the value of: (I'm supposed to answer this on paper, of course, no computer) 这个问题问我值:(我应该在纸上回答这个问题,当然,没有计算机)

*(gPtr1 + 3)
*(gPtr2 + 3)

Normally, I know for 1-dimensional array it adds an "address" instead of value. 通常,我知道对于一维数组,它添加一个“地址”而不是值。 So for example: 因此,例如:

int  balance[10]={1,2,6,4};
int *p=balance;  //p points to balance [0]

p+=3;           //points to balance [3]

However, I cant seem to make a pointer to a 2D array to test this out as in the question, I always get an "Initialization from incompatible pointer type" error. 但是,我似乎无法制作指向2D数组的指针来进行测试,因为在问题中,我总是收到“来自不兼容指针类型的初始化”错误。

The correct records will look like 正确的记录看起来像

int ( *gPtr1 )[3] = group1;
int ( *gPtr2 )[3] = group2;

And these expressions 这些表情

*(gPtr1 + 3)
*(gPtr2 + 3)

are trying to access the memory after the last elements of the arrays because the arrays have only three rows. 正在尝试访问数组的最后一个元素之后的内存,因为数组只有三行。 That is the type of expression *(gPtr1 + 3) is int[3] and there are only three such elements in the oridinal arrays. 这就是表达式*(gPtr1 + 3)int[3]并且在原始数组中只有三个这样的元素。

or you could write 或者你可以写

int *gPtr1 = ( int * )group1;
int *gPtr2 = ( int * )group2;

In this case using the pointers the arrays are interpretated as one-dimensional arrays with 9 elements and expressions 在这种情况下,使用指针将数组解释为具有9个元素和表达式的一维数组

*(gPtr1 + 3)
*(gPtr2 + 3)

will return correspondingly 将相应地返回

1
2

To access the address of group1 pass it to the pointer in this way: 要访问group1的地址,可以通过以下方式将其传递给指针:

int *gPtr1 = &group1[0][0];

Using that it's easy to find the soultions: 1 and 2. 使用它很容易找到灵魂:1和2。

Maybe this will help you get started: 也许这将帮助您入门:

#include <stdio.h>

int group1[3][3] = {3,4,5,1,9,8};
int group2[3][3] = {{1},{2,3},{4,5}};

int main( void )
{
    int *p;
    int i;
    for (p = group1[0], i = 0; i < 9; ++i, ++p )
        printf( "%d - ", *p);
    printf( "\n" );
    for (p = group2[0], i = 0; i < 9; ++i, ++p )
        printf( "%d - ", *p);
    printf( "\n" );
    return 0;
}

If you understand how memory in that 2D array is stored, it should be clear why this will work. 如果您了解该2D阵列中的内存是如何存储的,那么应该清楚为什么会起作用。 Since I assume that understanding is what you're looking for, I'll leave you to figure that part out on your own. 由于我假设理解是您要寻找的东西,因此我将让您自己解决这一问题。 :-) :-)

Two-dimensional matrices are represented in row-major order in memory. 二维矩阵在内存中以行优先顺序表示。 This is because memory is one-dimensional, and so it stores two-dimensional arrays as a concatenated list of rows. 这是因为内存是一维的,因此它将二维数组存储为行的连接列表。

When you create group1, you get an array of integers that is 9 long, and it looks like: 创建group1时,您将得到一个9长的整数数组,它看起来像:

3 4 5 1 9 8 _ _ _

So, when you do *(gPtr1 + 3) , it adds 3 to the original location of gPtr1 , which is the address of 3 . 因此,当您执行*(gPtr1 + 3) ,会将3加到gPtr1的原始位置,即3的地址。 Adding 3 gets you to the location of 1 , which you dereference. 3加到您要取消引用的1位置。

So, when you create group2, you get an array of integers that is 9 long, and it looks like: 因此,当您创建group2时,会得到一个长9的整数数组,它看起来像:

1 _ _ 2 3 _ 4 5 _

So, when you do *(gPtr2 + 3) , it adds 3 to the original location of gPtr2 , which is the address of 1 . 因此,当您执行*(gPtr2 + 3) ,会将3加到gPtr2的原始位置,即地址1 Adding 3 gets you to the location of 2 , which you dereference. 3加到您要取消引用的2位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM