I am having a hard time with a problem. Basically, I am to create a pointer to a 2-dimensional array of ints, say 3x3, and then this pointer will be used as a parameter when a function is called. I would post code but it is so messed up I don't think it would be useful. I've been reading widely conflicting things on the internet about how to code this. I am having trouble with making the prototype, definition, and function call work together.
Any help would be very appreciated! I can supply more info if needed. Thank you!
P.S. Some people say this is dumb because an array is technically already a pointer, but the assignment calls for a pointer to an array so that's what I need to do.
The easiest way it to create a pointer of the same data type and then to pass the starting address of the array into it.
1 2
int array[3][3];
int *ptr = array;
The last line passes the address of the starting position of the array into ptr. Now you need to access the array using the pointer.
1 2 3 4 5
for(size_t i = 0; i < 3; i++)
{
for(size_t j = 0; j < 3; j++)
*(ptr + i +j) = i + j;
}
This way you can access or modify the data in the array by using a pointer. Now as you said the array is also a pointer so you can also use the following code.
1 2 3 4 5
for(size_t i = 0; i < 3; i++)
{
for(size_t j = 0; j < 3; j++)
*(array + i +j) = i + j;
}
If you need any further assistance then do contact me.