Pointer to multidimensional array

This is a discussion on Pointer to multidimensional array within the C++ Programming forums, part of the General Programming Boards category; Hello, I want to write a function which returns a multidimensional array of bools. I gather that you cannot return ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    36

    Pointer to multidimensional array

    Hello,

    I want to write a function which returns a multidimensional array of bools. I gather that you cannot return an array in C++, but you can return a pointer to array. I have tried this, and it works fine when my array has a single dimension - but I want it to have two dimensions.

    Code:
    bool** myFunction(bool myArray[5][10])
    {
    // Do some stuff to myArray
    
    bool** myPointer;
    myPointer = myArray;
    return myPointer;
    }
    However, when I compile this, I get the following error:
    "error C2440: 'initializing' : cannot convert from 'bool [5][10]' to 'bool **"

    Please can anyone show me how I can return this pointer in the correct manner?

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is correct, you can not convert a 2D array into a pointer to pointer - they are not the same thing. The memory used by that 2D array is simply a 1D array, with information provided by the programmer to tell the compiler how to calculate the actual position of myarray[y][x].

    So the compiler would not be able to provide a char ** from this...

    Also, if you where to do:
    Code:
    bool **p;
    
    p = myFunction(someArray);
    p[2][3] = false;
    then the compiler will try to find the third pointer in an array of pointers ([2] is the third element), and then use the value in that memory to get to the fourth element on from that location. Since you are not actually returning the address of an array of pointers, the first step will use some random data from whatever happens to be after your pointer, and then we're definitely in "undefined-land", where anything that happens is completely unpredictable (makes Alice in Wonderland seem sane).

    --
    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.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    36
    Ok thanks, that makes sense. But in that case, what do I do if I need to change the values of the elements in myArray, and return myArray at the end of the function??

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why do you need to RETURN myArray - all arrays are passed as a pointer to the first element, so anything you change in the array will be modified in the original array, not the "copy" in the function [because there is no copy].

    --
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Since this is C++:
    Code:
    vector< vector<bool> > MyFunc()
    {
        vector< vector<bool> > v;
        // Do something with v;
        return v;
    }
    Or you can change bool to char since bool is a special case 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  2. pointer to multidimensional array
    By Bigbio2002 in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2006, 09:29 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21