I'm teaching myself a little C and have run across an exercise that I want to make sure I understand completely. The exercise is asking me to pass a pointer to a multidimensional array of integers to a function and iterate through the array. So I started with a print function before moving on to one taking input to populate the array. I tried all sorts of things but after finding a bit of code on type casting and iterating pointers I have the below program that seems to work but I'm not quite understanding what is going on. In my comments I have questions labeled 1-3, referring to the questions below the code. I was hoping someone more intelligent than myself could enlighten me.
//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
}
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]
)? 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."?
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?
So
1: pass pointer as assigned -> myArr[0][0]
then print the values in myArr[0][0]
, myArr[0][1]
, myArr[0][2]
, and myArr[0][3]
, then increment pointer myArr[1][0]
2: pass pointer as assigned ->myArr[1][0]
then print the values in myArr[1][0]
, myArr[1][1]
, myArr[1][2]
and myArr[1][3]
increment pointer to myArr[2][0]
3: pass pointer as assigned ->myArr[2][0]
then print the values in myArr[2][0]
, myArr[2][1]
, myArr[2][2]
and myArr[2][3]
increment pointer to myArr[3][0]
4: pass pointer as assigned ->myArr[3][0]
then print the values in myArr[3][0]
, myArr[3][1]
, myArr[3][2]
and myArr[3][3]
increment pointer to myArr[4][0]
and if this is the case what is the pointer pointing to since there shouldn't be a myArr[4][0]
?