First of all, I have gone through these links to better help me understand multidimensional arrays and how to pass them in functions
https://stackoverflow.com/a/17569578/10452758
http://c-faq.com/aryptr/pass2dary.html
https://jameshfisher.com/2016/12/08/c-array-decaying/
I still have some unclarity in my mind related to these things, which I would like to ask
Suppose we have a 2d array like
int mat[][COLS] = {{...}}
i) We know that we can pass the mat
in a function if its size is fixed i.e. known at the compile time as int (*mat)[COLS]
to a function.
Here, mat
is referred to as a single pointer to an integer array i.e. it points to a whole integer array, then will mat
in the main function like defined above will be referred to as same i.e. single pointer to an integer array or is it not a pointer at all??? But printing mat
into console gives out an address so basically, it is a pointer.
As in the case of int arr[] = {}
, we say that arr
is the address of the first element, what can be said about mat
.
I understand the difference b/w mat
and mat[0]
but how come a pointer pointing to the whole array and a pointer pointing to the first element of an array has the same value. How does in case of dereferencing the pointer, one knows that it is a pointer to the first element or the whole array???
I know the question sounds a little vague but simply I want to know why mat
and mat[0]
points to the same address of the memory because if they mean different thing won't they have different values???
If someone can share what they know about what mat
refers to as a pointer, it will be very helpful.
I have read other SO questions about these topics but these things still hadn't clicked with me.
ii) We know that arrays in C are not basically a pointer but when we access the value of an array, it decays to a value of a pointer type. So basically I wanted to ask is whether in int arr[] = {...}
, arr
is not a pointer and I want to back it with sizeof(arr)!=size_occupied_by_pointer
, is that the case also when we define mat
and when we print them in console there decayed pointer values are referred to. Is this the answer to the first question???
iii) Other doubt I have is passing a matrix as a pointer to a pointer. This is the code I am tending to follow but results in segmentation error. If someone can point out the mistake -
void process_pointer_to_pointer(int **arr, size_t rows, size_t cols){
for (size_t i = 0; i < rows; ++i){
std::cout << i << ": ";
for (size_t j = 0; j < cols; ++j)
std::cout << arr[i][j] << '\t';
std::cout << std::endl;
}
}
int main(int argc, char const *argv[]){
const int m=4, n=4;
int mat[][n]= {
{1, 3, 1, 5},
{2, 2, 4, 1},
{5, 0, 2, 3},
{0, 6, 1, 2}
};
int *ip = &mat[0][0];
process_pointer_to_pointer(&ip, m, n);
I know the simpler way of doing the same thing by using an array of pointers and filling those with mat[i]
, but I have followed the limitation in following this method and tried this approach from here
http://c-faq.com/aryptr/pass2dary.html
but didn't get the desired result...
PS: The question may be completely all over the place so if not able to understand something specific, pls ask me. I am more than happy to go in a debate mode. Also, I am new to coding, so pls cut me some slack. :p