Thread: Passing a pointer to two-dimension array in a function

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    7

    Passing a pointer to two-dimension array in a function

    I am having no problem in passing a pointer to a one dimension array in a function in my code. However, when I tried passing a pointer to a two-dimension array im receiving an error. Can somebody help or explain?


    Code using one-dimension array:

    Code:
    #include <iostream>
    
    
    void InitializeArrays(double *array1, double *array2)
    {
    
    }
    
    
    int main()
    {
    
    double Array_A[5], Array_B[5];
    
    InitializeArrays(Array_A, Array_B);
    
    
    return 0;
    
    }
    Code using two-dimension array:

    Code:
    #include <iostream>
    
    
    void InitializeArrays(double *array1, double *array2)
    {
    
    }
    
    
    int main()
    {
    
    double Array_A[5][5], Array_B[5][5];
    
    InitializeArrays(Array_A, Array_B);
    
    
    return 0;
    
    }
    Error encountered: error C2664: 'InitializeArrays' : cannot convert parameter 1 from 'double [5][5]' to 'double *'

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When passed as an argument, a double[5][5] becomes a pointer to a double[5], which is quite different from a pointer to a double. A solution is to change the function to:
    Code:
    void InitializeArrays(double (*array1)[5], double (*array2)[5])
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    you should probably listen to laserlight, but something like this would work also:

    Code:
    void InitializeArrays(double **array1, double **array2,size_t a1l1,size_t a1l2,size_t a2l1,size_t a2l2)
    {
    
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by m37h0d View Post
    you should probably listen to laserlight, but something like this would work also:

    Code:
    void InitializeArrays(double **array1, double **array2,size_t a1l1,size_t a1l2,size_t a2l1,size_t a2l2)
    {
    
    }
    No, because you do not have a pointer to a pointer in the first place, you have the address of a two-dimensional array. To make that work, you'd have to create a one-dimensional array of pointers that point to the first element of each row of data.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    I was having trouble cutting my grass by swinging my lawnmower blade about...it worked much better in the lawnmower.

    Code:
    class Matrix {
        double[5][5];
    public:
        Matrix() { // initialize array here }
    };

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Better would be vector of vector and pass by reference.
    Then you wouldn't have to guess what type must be placed in the function declaration.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by matsp View Post
    No, because you do not have a pointer to a pointer in the first place, you have the address of a two-dimensional array. To make that work, you'd have to create a one-dimensional array of pointers that point to the first element of each row of data.

    --
    Mats
    yes, you're correct. that's what i ended up having to do when i was in this sort of position...

    is there any other way to pass a multidimensional array without specifying the lengths as a literals?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by m37h0d View Post
    is there any other way to pass a multidimensional array without specifying the lengths as a literals?
    No, because the compiler wouldn't be able to generate the correct code, then.
    But it's possible with classes and objects, say a vector of vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    No, because the compiler wouldn't be able to generate the correct code, then.
    But it's possible with classes and objects, say a vector of vector.
    It is possible if you degenerate the array to a single dimension array [which is essentially how the compiler goes about solving that problem].

    It is not "possible with objects" - but objects can hold information to describe their own contents, so you can hide the information about the array dimensions inside the class.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    It is possible if you degenerate the array to a single dimension array [which is essentially how the compiler goes about solving that problem].
    True, but this would also make it a 1D array. But a 2D array can also be represented as a 1D array, so it's possible, but it isn't exactly the same.

    It is not "possible with objects" - but objects can hold information to describe their own contents, so you can hide the information about the array dimensions inside the class.
    That's more to the point I was referring to. Classes can hide things such as their dimensions (such as vector) and enable passing themselves in such a way that they can remain 2D-arrays without passing the actual dimensions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    it is curious to me that you can treat a pointer-to-pointer like a two dimensional array (provided it's been properly initialized), but the inverse isn't true.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They are of different nature.
    A dynamic 2D array is like a linked list:
    Code:
    A*:
        B*:
            C
            D
        E*:
            F
            G
    But 2D arrays are sequential:
    Code:
    A
    B
    C
    D
    E
    F
    G
    So it wouldn't work.
    In the dynamic array, you'd dereference the first point with the first dimension which in turn would have more pointers to the second dimension.
    But in normal 2D arrays, there are no pointers to dereference.
    Last edited by Elysia; 06-19-2008 at 09:59 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM