简体   繁体   English

这在C int(* ptr [2])[4]中是什么意思;

[英]what does this mean in C int (*ptr[2])[4];

I want to know what is the meaning of the definition 我想知道定义的含义是什么

int (*ptr[2])[4];

If i print the size of "ptr", it displays 16. Also on my machine size of integer pointer is giving 8, where as size of integer is 4. 如果我打印“ ptr”的大小,它将显示16。在我的机器上,整数指针的大小也为8,其中整数的大小为4。

If i redifine ptr as , 如果我将ptr重新命名为,

int (*ptr[2]); then also, size of ptr is 16.

so questions are 所以问题是

  1. what is the meaning of definition int (*ptr[2])[4]; int(* ptr [2])[4]的定义是什么?
  2. How do i initialize ptr? 如何初始化PTR?

Use the right-left rule to read such declarations. 使用左右规则读取此类声明。

int (*ptr[2])[4] is an array of 2 pointers to arrays of 4 integers. int (*ptr[2])[4]是2个指针数组,指向4个整数的数组。

If i print the size of "ptr", it displays 16. Also on my machine size of integer pointer is giving 8 如果我打印“ ptr”的大小,它将显示16。同样在我的机器上,整数指针的大小为8

This is because ptr is an array of 2 pointers. 这是因为ptr是2个指针的数组。

Q2 How do i initialize ptr? Q2 如何初始化PTR?

Eg, 例如,

int a[4] = { 1, 2, 3, 4};
int b[4] = { 6, 7, 8, 9};
int (*ptr[2])[4] = { &a, &b };
int i;
for(i = 0; i<4;++i){
    printf("%d ", (*ptr[0])[i]);//a
}
printf("\n");
for(i = 0; i<4;++i){
    printf("%d ", (*ptr[1])[i]);//b
}
printf("\n");

,

int (*ptr[2])[4]; is an array which has 2 elements, the type of each element is int (*) [4] , that stands for an pointer which point an array which has 4 element. 是一个具有2个元素的数组,每个元素的类型为int (*) [4] ,它表示一个指针,该指针指向具有4个元素的数组。

this is equal to typedef int (* PTR) [4]; PTR ptr[2]; 这等于typedef int (* PTR) [4]; PTR ptr[2]; typedef int (* PTR) [4]; PTR ptr[2];

typedef int (*arrayOfNPointers)[N];

So, if this is right, it is a data type describing an array of N pointers to int.

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

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