Array 2D and pointer

Hi all,

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 (*)[(((long unsigned int)(((long int)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?

Can any one give a help? Thanks a lot!
Last edited on
Perhaps first running and then understanding this snippet would help you arrive at an answer to the questions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

int main()
{
    enum { N = 100 } ;

    typedef double 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 :-)

This article is somehow interesting,
http://www.cplusplus.com/forum/articles/9/
The memory location of the pointer is not the first index of the array.
1
2
3
char arr[10];  // Uses ten bytes

char *arr = new char[10];  // Uses ten bytes plus the 4/8 byte pointer 
chucthanh wrote:
‘double (*)[(((long unsigned int)(((long int)N) - 1)) + 1u)]’

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)
Last edited on
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 :-)
I will switch to pointers instead of arrays

I will switch to std::vector<> instead of arrays
http://www.cprogramming.com/tutorial/stl/vector.html
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.
Topic archived. No new replies allowed.