取消引用指针指向整个数组 [英] Dereferencing pointer to entire array
问题描述
我在学习指针(全部)阵列的温度。
假如我宣布整数的二维矩阵:
INT ARR [3] [3] =
{
{1,2,3},
{4,5,6},
{7,8,9}
};
现在,我宣布一个适当类型的指针:
INT(* PTR)[3] [3];
初始化:
PTR =安培;编曲;
现在,PTR包含ARR的地址。
PTR --->及(ARR [0] [0] ARR [0] [1]常用3 [0] [2]
改编[1] [0]改编[1] [1]常用3 [1] [2]
ARR [2] [0]常用3 [2] [1] ARR [2] [2])
所以,当我们解引用PTR,它应该产生的第一个位置,不是吗?
的printf(%d个,* PTR)
给在编译时错误。要打印的第一要素,我已经使用 *** PTR
。
我有2个问题:
- 我不明白三合为一的取消引用的需要。
- 如果
* PTR
不指向数组的第一个元素,这是什么点
什么?
的printf(* PTR);
是错误的,因为第一个参数的printf
必须指定要使用的格式打印的参数,其余的字符串。要打印的地址,使用:
的printf(%p,* PTR);
我不明白三合为一提领该需求。
块引用>类型
P
是INT(*)[3] [3]
。类型的
* P
是INT [3] [3]
。类型的
** P
是INT [3]
类型的
*** P
是INT
。当你有一个这样的指针,最好使用数组语法。
(* P)[0] [0]
通用的形式是:
(* P)[i] [j]的
这是一个很多比使用更少的混乱
*** P
。此外,*** P
可用于访问[0] [0] -th
数组元素只有
如果
* PTR
不指向数组的第一个元素,这是什么指向?
块引用>希望的答案previous问题解释这一点。
I'm learning pointers to (entire) arrays in C.
Suppose I declare a 2d matrix of ints:
int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
Now, I declare a pointer of appropriate type:
int (*ptr)[3][3];
Initialize it:
ptr = &arr;
Now, ptr contains the address of arr.
ptr --->&(arr[0][0] arr[0][1] arr[0][2] arr[1][0] arr[1][1] arr[1][2] arr[2][0] arr[2][1] arr[2][2])
So, when we dereference ptr, it should yield the first location, isn't it?
printf("%d", *ptr)
gives an error at compile time. To print the first element, I've to use***ptr
.I have 2 questions:
- I can't understand the need for a triple dereferencing.
- If
*ptr
doesn't point to first element of array, what does it point to?
解决方案printf(*ptr);
is wrong since the first argument to
printf
needs to be a string that specifies the format to use to print the rest of the arguments. To print the address, use:printf("%p", *ptr);
I can't understand the need for a triple dereferencing.
The type of
p
isint (*)[3][3]
.The type of
*p
isint [3][3]
.The type of
**p
isint [3]
The type of
***p
isint
.When you have a pointer like that, it's best to use the array syntax.
(*p)[0][0]
The generic form would be:
(*p)[i][j]
That is a lot less confusing than using
***p
. Besides,***p
can be used to access the[0][0]-th
element of the array only.If
*ptr
doesn't point to first element of array, what does it point to?Hopefully the answer to the previous question explains this.
这篇关于取消引用指针指向整个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!