简体   繁体   English

指向2D数组的指针C,C ++

[英]Pointers to 2D arrays C, C++

I'm learning about pointers: 我正在学习指针:

int x[10]; 
int *p = &x

this would make a pointer type int to the first element. 这会使指针类型为第一个元素的int So, if I had 2D array, I need to use double pointer: first pointer would point to the second dimension of the array. 所以,如果我有2D数组,我需要使用双指针:第一个指针指向数组的第二个维度。 This means : 这意味着 :

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

and when I want to point to it I must declare the size of the second dimension like this, right ? 当我想指出它时,我必须像这样声明第二维的大小,对吧?

int *p[4] = x;

or there is another way by typing : int **p ; 或者通过输入另一种方式: int **p ; ?

and int *p[4] is array of integer pointers which takes 4 * (sizeof(int*)) , right? int *p[4]是整数指针数组,取4 * (sizeof(int*)) ,对吧?

this would make a pointer type (int) to first element .. 这会使指针类型(int)成为第一个元素..

No. 没有。
&x is the address of array x and is of type int (*)[10] and you can't assign it to a int * type. &x是数组x的地址,类型为int (*)[10] ,您不能将其分配给int *类型。 Both are incompatible types. 两者都是不兼容的类型。

So, if I had 2D array, I need to use double pointer: first pointer would point to the second dimension of the array. 所以,如果我有2D数组,我需要使用双指针:第一个指针指向数组的第二个维度。

No. 没有。
In expressions, arrays converted to pointer to its first elements except when an operand of sizeof and unary & operator. 在表达式中,除了sizeof和unary & operator的操作数之外,数组转换为指向其第一个元素的指针。 Therefore, in this case the type of x will be int (*)[4] after conversion. 因此,在这种情况下,转换后x的类型将为int (*)[4] You need a pointer to an array of 4 int instead of an array of 4 pointers 你需要一个指向4 int数组的指针而不是4指针数组

 int (*p)[4] = x;

To add, the first example is not correct. 要添加,第一个示例不正确。

x is an array of 10 ints. x是10个整数的数组。 p is a pointer to int and not a pointer to an array of 10 ints. p是指向int的指针,而不是指向10个int的数组的指针。 When assigned to p , x decays to the type pointer to int. 当赋值给px衰减到int的类型指针。

The assignment should be simply: 任务应该简单:

int* p = x;

Example program: 示例程序:

#include <stdio.h>
main(int argc, char* argv[])
{
    int x[3][4] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 987, 9, 9 } }; 
    int(*p)[4] = x; printf("p[2][1] %d", p[2][1]); 
    printf("\nsizeof(p) is : %d \nsizeof(*p) is : %d\n", sizeof(p), sizeof(*p)); 
}

Output 产量

p[2][1] 987
sizeof(p) is : 4
sizeof(*p) is : 16

In my system (as in yours) int and pointers are 32 bits. 在我的系统中(如在你的系统中)int和指针是32位。 So the size of a pointer is 4 and the size of an int is 4 too. 所以指针的大小是4,int的大小也是4。

p is a pointer first of all. p首先是指针。 Not an array. 不是数组。 A pointer. 一个指针。 It's size is 4 no less no more. 它的大小不超过4。 That's the answer to your question 这就是你问题的答案

Now, just to add some useful information: 现在,只是添加一些有用的信息:

p is a pointer to an array of 4 integers. p是指向4个整数数组的指针。 The size of what p points to is 4*4==16. p指向的大小是4 * 4 == 16。 (Try to change int to short in the example program, you'll have sizeof(*p) is : 8 ) (尝试在示例程序中将int更改为short ,你将sizeof(*p) is : 8

I can assign p=x because the type is correct, now p contains the address of x and p[0] is the same as x[0] (the same array of 4 int). 我可以指定p=x因为类型是正确的,现在p包含x的地址,p [0]与x [0]相同(4 int的相同数组)。 p[2][3] is the same as x[2][3] and *(p[2]+3) . p[2][3]x[2][3]*(p[2]+3) p[2] points to the element 2 of x and p[2]+3 points to the element 3 of x[2] . p[2]指向x的元素2, p[2]+3指向x[2]的元素3。 (all indexing is 0 based) (所有索引都是0)

// Here is not complicated matrix of size
int p[2][4]={
              {1,2,3,4},
              {5,6,7,8}
            };
// points to the first Elements :: ptr
int (*ptr)[0] = &p;
// Now have the same adresse ( ptr and p )
printf("Hello world! %d \n",ptr[1][3]); // show  4 

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

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