I get the fact that arrays are NOT the same as pointers but when an array is used as an rvalue it is implicitly converted to
a pointer. I used the above to write a program to store a 1-D array's address in a pointer variable. Below is my code:
#include<iostream>
int main()
{
std::cout<<"Size of int: "<<sizeof(int)<<" "<<std::endl;
int arr[7];
int *ptr;
ptr = arr;
for(int i=0;i<7;i++)
std::cout<<ptr++<<" ";
return 0;
}
The above works alright but when I declare a 2D array and try to assign it to a pointer to a pointer it throws an error: "cannot
convert int[][] to int **. below is the code:
#include<iostream>
int main()
{
std::cout<<"Size of int: "<<sizeof(int)<<" "<<std::endl;
int arr[7][3];
int **ptr;
ptr = arr;
for(int i=0;i<7;i++)
std::cout<<ptr++<<" ";
return 0;
}
When I do the above in C I get a warning: "assignment from incompatible pointer type [enabled by default]."
#include<stdio.h>
int main()
{
int arr[7][4];
int **ptr;
ptr = arr;
return 0;
}
What is going on? Why is a pointer to anoter pointer an incompatible type for a 2-D array? Why is C++ throwing an error
whereas C only gives me a warning? Furthermore, in a 2D array are all the row elements poninter to another pointer?
Thanks in advance for answering my questions!