I have been stuck with wondering about the difference between double** px and double px [N][N] . I thought they are the same (except for const qualified), but then I could not understand why this call doesn't work
1 2 3 4
//In main function
double m [N]; //Magnetisations
double C [N][N]; //Correlations
bp (m, C);
where function bp is
void bp (double* m, double** C) {//...};
the error was
mp.cpp:134: error: cannot convert ‘double (*)[(((longunsignedint)(((longint)N) - 1)) + 1u)]’ to ‘double**’ for argument ‘2’ to ‘void bp(double*, double**)’
And a minor question is why in calling functions, the size of the second dimension of 2D array should be declared for the compiler to know? Like double a[][N], I can not think of any wrong with double a[][], or one can always use double **a?
#include <iostream>
int main()
{
enum { N = 100 } ;
typedefdouble array_t[N] ; // array_t => array of 100 double
array_t a ; //Magnetisations => double a[N] ; //Magnetisations
double* pa = a ; // a is implicitly converted to a pointer to the first element
std::cout << "pa: " << pa << '\n' ;
std::cout << "sizeof(double): " << sizeof(double) << '\n' ;
// to invcrement the pointer, the size of each element must be known
// it is sizeof(double)
++pa ; // pa is incremented, now points to the second element
std::cout << "pa: " << pa << "\n\n" ;
typedef array_t array2d_t[N] ; // array2d_t => array of 100 array_t
// array of array of 100 double
array2d_t b ; // array_t b[N] => double b[N][N] ;
array_t* pb = b ; // b is implicitly converted to a pointer to the first element
double (*pb2)[N] = b ; // b is implicitly converted to a pointer to the first element
// the first element is array of 100 double
std::cout << "pb: " << pb << '\n' ;
std::cout << "sizeof(double[100]): " << std::hex << std::showbase
<< sizeof(double[100]) << '\n' ;
// to invcrement the pointer, the size of each element must be known
// it is sizeof( array_t ) or sizeof( double[100] )
++pb ; // pb is incremented, now points to the second element
// ie. the seconsd array_t; the second array of 100 double
std::cout << "pb: " << pb << '\n' ;
}
Thanks! Maybe I understand the main point: "pointers are not arrays, and arrays are not pointers".
It just happened that so many tutorials tell they are kind of the same, so I take it with out any careful consideration. In fact, I always use pointers, very hardly use arrays, except for today :-)
Incidentally, there's another error hiding here: this error message indicates that N in your code is not a constant expression, but, based on file name, you're expecting to write a C++ program. Your array declarations only compiled due to a non-portable language extension. Try compiling with -pedantic (since this seems to be gcc)
The memory location of the pointer is not the first index of the array.
Actually that seems clear to me. The other way around "Array is not the (constant) pointer to the first element of a continuous memory region declared" is less clear.
Incidentally, there's another error hiding here: this error message indicates that N in your code is not a constant expression, but, based on file name, you're expecting to write a C++ program. Your array declarations only compiled due to a non-portable language extension. Try compiling with -pedantic (since this seems to be gcc)
Thanks! I see what you meant. I will switch to pointers instead of arrays (permanently), and hopefully there will be no problem in the future :-)
Oh yes, but my boss uses C (not C++), so I should use something meaningful to him.
By the way, if C++ also has "matrix", that would be great! It helps unifying a lot of scientific libraries. Right now it seems to me that each one has his own 2D array library (TNT, GNU, RCpp...). That, D seems to be doing, but until now D does not seem to be so popular.