我正在自学一点 C,并且遇到了一个练习,我想确保自己完全理解。练习要求我将指向多维整数数组的指针传递给函数并遍历该数组。所以我从一个打印函数开始,然后继续使用输入来填充数组。我尝试了各种各样的事情,但是在找到一些关于类型转换和迭代指针的代码之后,我有下面的程序似乎可以工作,但我不太明白发生了什么。在我的评论中,我有标记为 1-3 的问题,指的是代码下方的问题。我希望比我更聪明的人能启发我。
//passing multi-Array to a simple print function
#include <stdio.h>
//simple print function prototype
void printMyArr(int (*pt)[4]);
int main()
{
//declare and initialize a multi-array. I picked 4x4 and some arbitrary
//numbers for simplicity's sake
int myArr[4][4] = { {12, 16, 19, 20},
{5, 99, 102, 200},
{12, 20, 25, 600},
{65, 66, 999, 1000} };
//declare a pointer to an array of integers and an int for a loop counter
int (*pt)[4], counter;
//initialize the pointer
pt = myArr;
//for loop calling printMyArr function to iterate through arrays -- or this is what I understand it to mean
for(counter=0; counter<4; counter++)
printMyArr(pt++); //<-------------Question 1
return 0;
}
//function called to print array elements to the console
void printMyArr(int(*pt)[4])
{
//declare a counter....and apparently another pointer
int counter, *p;
//initialize new pointer to old pointer and type cast array as int
p = (int *)pt; //<-------------Question 2
//for loop to iterate through elements of array -- or this is what I understand it to mean
for(counter=0; counter<4; counter++)
printf("\n\n\n%d", *p++); //<------Question 3
}
问题 1:在这里,我将指针传递给了函数,我一直在想,“这个循环迭代的是什么?”。我是否正确地认为我正在递增指向每个数组中第一个元素的指针(myArr[0][0]
,myArr[1][0]
, myArr[2][0]
, myArr[3][0]
)?此外,我是否正确地假设该行的语法本质上是在说:“执行传递当前指针的函数,然后在完成后递增指针。”?
问题2:这是我最困惑的地方。经过相当多的挖掘后,我发现这一点可以使其正常运行,我意识到这就是它的工作原理,但为什么呢?
问题 3:我认为我在这里递增每个元素是否正确?
所以
1: 按指定传递指针 -> myArr[0][0]
然后打印 myArr[0][0]
中的值,myArr[ 0][1]
、myArr[0][2]
和 myArr[0][3]
,然后递增指针 myArr[1 ][0]
2:传递分配的指针 ->myArr[1][0]
然后打印 myArr[1][0]
中的值,myArr[ 1][1]
、myArr[1][2]
和 myArr[1][3]
将指向 myArr[2][ 的指针递增0]
3:按指定传递指针 ->myArr[2][0]
然后打印 myArr[2][0]
中的值,myArr[ 2][1]
、myArr[2][2]
和 myArr[2][3]
将指针递增到 myArr[3][ 0]
4: 按指定传递指针 ->myArr[3][0]
然后打印 myArr[3][0]
中的值,myArr[ 3][1]
、myArr[3][2]
和 myArr[3][3]
将指针递增到 myArr[4][ 0]
,如果是这种情况,指针指向什么,因为不应该有 myArr[4][0]
?
最佳答案
Question 1: Here, I've passed the pointer to the function and I keep thinking, "What is this loop iterating over?". Am I correct in thinking that I am incrementing the pointer to each of the first elements in each array (myArr[0][0], myArr[1][0], myArr[2][0], myArr[3][0])?
或多或少。 pt
从myArr
的地址开始。你知道数组中有 4 个东西,所以你循环 4 次,每次访问它后递增 pt
(见下文)以传递给 printMyArr
的 4 个元素中的每一个“顶层”、“外层”数组。然后 printMyArr
遍历内部数组的 4 个元素以显示每个数字。
Also, am I correct in assuming that the syntax of this line is in essence saying: "Execute the function passing the current pointer and THEN when it's done increment the pointer."?
功能上,是的。从技术上讲,操作顺序如下所示:
1) 获取pt
的值
2) 递增 pt
3) 调用函数,传递步骤 1 中 pt
的先前值
pt
作为 pt++
调用的一部分递增,但 pt++
计算为旧值。作为函数的参数,pt++
必须在传递给它的函数运行之前 求值。但是时间看起来与在针对您的案例运行函数后直接进行评估的时间相同。
让我们把问题 2 和问题 3 放在一起,因为它们都是答案的一部分。
Question 2: This is what has me the most confused. After quite a bit of digging I found this bit to make it run right and I realize this is how it works, but why?
Question 3: Am I correct thinking that I am incrementing each element here?
p = (int *)pt;
for(counter=0; counter<4; counter++)
printf("\n\n\n%d", *p++);
p
存储一个整数的地址。您将它设置为内部数组第一个元素的地址(可以说是内部数组本身的地址)。然后,知道数组中有 4 个元素,就可以循环 4 次,对 p
执行类似于对 pt
执行的指针后递增操作外部数组。
第一次迭代,p
与pt
相同,是内存中4个整数值中第一个的地址。 *p
,取消引用指针,获取第一个整数。 *p++
,由于操作顺序(++ 具有最高优先级,取消引用 * 较低),返回 p
处的整数,留下 p
指向下一个循环的下一个整数地址。
一般来说,casting values in C should be avoidid whenever possible .您只需在此处取消引用以将 p
指向整数集。 pt
保存内存中连续的 4 个整数集(内部数组)的 4 个地址(外部数组)之一的地址。 *pt
处的值是内存中 4 个连续整数的“当前”集合的地址。所以你可以简单地说,
int* p=*pt;
它不会随意转换类型,而且非常简单明了。 (感谢@WhozCraig)
关于c - 具有迭代数组函数的优先级、括号、指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54012242/