Simple question: Cannot convert 2D array to pointer-to-pointer

Discussion in 'C++' started by overbored, Sep 11, 2004.

  1. overbored

    overbored Guest

    I can do this:

    int asdf[2];
    int* zxcv = asdf;

    but not this:

    int asdf[2][2];
    int** zxcv = asdf;

    or I get 'cannot convert from int[2][2] to int**'. Does anybody know why
    this is, and how to get around it? I just want a pointer to that double
    array. Thanks in advance.
    overbored, Sep 11, 2004
    #1
    1. Advertising

  2. "overbored" <> wrote in message
    news:Xns95617AAFAB087yangstaoverbored@127.0.0.1...
    >I can do this:
    >
    > int asdf[2];
    > int* zxcv = asdf;
    >
    > but not this:
    >
    > int asdf[2][2];
    > int** zxcv = asdf;
    >
    > or I get 'cannot convert from int[2][2] to int**'. Does anybody know why
    > this is, and how to get around it?


    C++ says T[] converts to T*, it doesn't say anything about T[][] to T** and
    why should it? It would be impossible to implement. T[] converts to T*
    because x is equivalent to *(x + i). But x[j] is not equivalent to
    *(*(x + i) + j) because the pointer arithmetic doesn't work.

    > I just want a pointer to that double
    > array. Thanks in advance.


    Why? If you explain what you are trying to do then maybe someone can tell
    you how.

    You can do this

    int asdf[2][2];
    int (*zxcv)[2] = asdf;

    Is that good enough?

    john
    John Harrison, Sep 11, 2004
    #2
    1. Advertising

  3. I don't understand your explanation. Why do you say the pointer
    arithmetic "wouldn't work"? Assuming x : int**,

    x
    +---+ +---+ +---+---+
    | |---->| |---->| 5 | 7 |
    +---+ +---+ +---+---+
    | |
    +---+
    int** int* int


    So,

    x : int** returns the address of the 0th element of the first array.

    x + i : int** returns the address of the ith element of the first array.

    *(x + i) : int* returns the value of the ith element of the first array,
    or the address of the 0th element in the second array.

    *(x + i) + j : int* returns the address of the jth element in the second
    array.

    *(*(x + i) + j) : int returns the value of the jth element in the second
    array.

    Can you explain what I'm missing? I'm not asserting anything; I'm sure
    there's a perfectly good reason why this casting is not allowed. I'm
    just not seeing it.

    As for my original question, I'm simply wondering how to do the
    following:

    int** asdf = new int[j];

    You can't apply the approach you suggested because you don't know the
    size of the array in at compile-time.

    "John Harrison" <> wrote in
    news::

    >
    > "overbored" <> wrote in message
    > news:Xns95617AAFAB087yangstaoverbored@127.0.0.1...
    >>I can do this:
    >>
    >> int asdf[2];
    >> int* zxcv = asdf;
    >>
    >> but not this:
    >>
    >> int asdf[2][2];
    >> int** zxcv = asdf;
    >>
    >> or I get 'cannot convert from int[2][2] to int**'. Does anybody know
    >> why this is, and how to get around it?

    >
    > C++ says T[] converts to T*, it doesn't say anything about T[][] to
    > T** and why should it? It would be impossible to implement. T[]
    > converts to T* because x is equivalent to *(x + i). But x[j] is
    > not equivalent to *(*(x + i) + j) because the pointer arithmetic
    > doesn't work.
    >
    >> I just want a pointer to that double
    >> array. Thanks in advance.

    >
    > Why? If you explain what you are trying to do then maybe someone can
    > tell you how.
    >
    > You can do this
    >
    > int asdf[2][2];
    > int (*zxcv)[2] = asdf;
    >
    > Is that good enough?
    >
    > john
    >
    >
    >
    Class Account, Sep 11, 2004
    #3
  4. overbored

    overbored Guest

    Sorry, that was me.

    Class Account <> wrote in
    news:Xns95618BDC31DF9cs184ajimaileecsberk@127.0.0.1:

    [snip]
    overbored, Sep 11, 2004
    #4
  5. overbored

    David Hilsee Guest

    "Class Account" <> wrote in message
    news:Xns95618BDC31DF9cs184ajimaileecsberk@127.0.0.1...
    > I don't understand your explanation. Why do you say the pointer
    > arithmetic "wouldn't work"? Assuming x : int**,
    >
    > x
    > +---+ +---+ +---+---+
    > | |---->| |---->| 5 | 7 |
    > +---+ +---+ +---+---+
    > | |
    > +---+
    > int** int* int
    >
    >
    > So,
    >
    > x : int** returns the address of the 0th element of the first array.
    >
    > x + i : int** returns the address of the ith element of the first array.
    >
    > *(x + i) : int* returns the value of the ith element of the first array,
    > or the address of the 0th element in the second array.
    >
    > *(x + i) + j : int* returns the address of the jth element in the second
    > array.
    >
    > *(*(x + i) + j) : int returns the value of the jth element in the second
    > array.
    >
    > Can you explain what I'm missing? I'm not asserting anything; I'm sure
    > there's a perfectly good reason why this casting is not allowed. I'm
    > just not seeing it.


    A two-dimensional array's elements are stored contiguously, not in separate
    arrays that are accessed via pointers that are stored contiguously. When
    you access elements using brackets (e.g. [4][5]), you are actually using a
    single (computed) offset from the first element. The same syntax for a
    pointer-to-pointer, on the other hand, uses one offset to locate a pointer,
    and then uses an offset from that pointer to locate the element desired.
    They are quite different.

    > As for my original question, I'm simply wondering how to do the
    > following:
    >
    > int** asdf = new int[j];
    >
    > You can't apply the approach you suggested because you don't know the
    > size of the array in at compile-time.

    <snip>

    See the FAQ (http://www.parashift.com/c -faq-lite/), Section 16 ("Freestore
    management"), question 15 ("How do I allocate multidimensional arrays using
    new?"). In fact, the whole section is a good read if you have questions
    about dynamically allocating objects.

    --
    David Hilsee
    David Hilsee, Sep 11, 2004
    #5
  6. overbored

    JKop Guest

    > As for my original question, I'm simply wondering how to do the
    > following:
    >
    > int** asdf = new int[j];


    First things first:

    int* a = new int[125];


    is lain out in memory EXACTLY as is:


    int (*b)[5][5] = new int[5][5][5];


    Now, for some reason you want a pointer to a pointer to that array.

    int** p_p_ints;

    Okay, so in this variable, we need to store that address of a pointer. But
    you don't have a pointer whose address to put in! Make one:

    int** p_p_ints; //Pointer to pointer to int

    int* p_ints; //pointer to int

    p_ints = new int[5][5][5];

    p_p_ints = &p_ints;


    Now p_p_ints points to a pointer which points to your array of ints. And why
    exactly do you want this?!


    Once you've defined an array like:

    int monkey[60];


    there are ways of accessing it as if it were:


    int monkey[2][30]

    int monkey[3][20]

    int monkey[4][15]

    int monkey[5][12]

    int monkey[6][10]


    -JKop
    JKop, Sep 12, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    337
    Tim Rentsch
    Oct 9, 2004
  2. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    663
    Martin Ambuhl
    Jan 28, 2008
  3. George2

    compile error cannot convert 'this' pointer

    George2, Feb 25, 2008, in forum: C Programming
    Replies:
    0
    Views:
    322
    George2
    Feb 25, 2008
  4. RSL
    Replies:
    14
    Views:
    3,216
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    438
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page