The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

C++ Multidimensional array [solved/lock]

SkyEyeSkyEye Registered User regular
edited April 2010 in Help / Advice Forum
I need an additional alias/reference to a multidimensional character array. I'm able to copy the first array with, char *A1 = A[0]; but when I try to apply that logic to higher dimensions like char **A1 = A; it doesn't compile. A[12][12] is just an array of pointers, right? Why isn't this working?

Steam: Autumn_Thunder - SC2: AutumnThundr.563 (NA) - Hearthstone: AutumnThundr.1383

SkyEye on

Posts

  • admanbadmanb unionize your workplace Seattle, WARegistered User regular
    edited April 2010
    Because char A[12][12] isn't an array of pointers, it's a series (in memory) of 144 chars.

    admanb on
  • SkyEyeSkyEye Registered User regular
    edited April 2010
    admanb wrote: »
    Because char A[12][12] isn't an array of pointers, it's a series (in memory) of 144 chars.

    I thought multidimensional arrays were "arrays of arrays?"

    SkyEye on
    Steam: Autumn_Thunder - SC2: AutumnThundr.563 (NA) - Hearthstone: AutumnThundr.1383

  • admanbadmanb unionize your workplace Seattle, WARegistered User regular
    edited April 2010
    C++ was built on C and C was built on speed. Since arrays are declared at a constant size it's much faster to allocate one giant chunk of memory for a mulitdimensional array and access values by basic arithmetic. i.e. for an array declared A[n][m] you access element a[j] with the formula:

    A[0][0] + (n * i + j) * size(A).

    admanb on
  • SkyEyeSkyEye Registered User regular
    edited April 2010
    Ok. So it's basically one big array. So how does one create another reference to the array?

    SkyEye on
    Steam: Autumn_Thunder - SC2: AutumnThundr.563 (NA) - Hearthstone: AutumnThundr.1383

  • admanbadmanb unionize your workplace Seattle, WARegistered User regular
    edited April 2010
    Like so: char (&A1)[12][12] = A.

    Google; 5 minutes.

    admanb on
  • SavantSavant Simply Barbaric Registered User regular
    edited April 2010
    SkyEye wrote: »
    I need an additional alias/reference to a multidimensional character array. I'm able to copy the first array with, char *A1 = A[0]; but when I try to apply that logic to higher dimensions like char **A1 = A; it doesn't compile. A[12][12] is just an array of pointers, right? Why isn't this working?

    char* A1 = A[0] doesn't copy the array at all. Also, what is your declaration for A here? What you're posting is fairly jumbled.

    Lets say that you do the following:
    int A[5] = {10, 11, 12, 13, 14};
    int* B = A;
    

    Then B is just a pointer to the location of memory where the first element of A is stored. So if you dereferenced B directly you would get 10, but B is not a copy of A. If you changed the data in A, what you would get out of dereferencing B would be different as well, and the same would be true if you changed the array by accessing it through B. Because they both would point to the same place where the underlying data resides.

    Here's a good place to go over some of the array basics: http://www.cplusplus.com/doc/tutorial/arrays/

    Savant on
  • admanbadmanb unionize your workplace Seattle, WARegistered User regular
    edited April 2010
    Since he said, "I need an additional alias/reference..." I think that's what he meant.

    admanb on
  • SkyEyeSkyEye Registered User regular
    edited April 2010
    admanb wrote: »
    Like so: char (&A1)[12][12] = A.

    Google; 5 minutes.

    I need to know where you found that; I looked through at least a dozen forums and articles and got zip.

    SkyEye on
    Steam: Autumn_Thunder - SC2: AutumnThundr.563 (NA) - Hearthstone: AutumnThundr.1383

  • SavantSavant Simply Barbaric Registered User regular
    edited April 2010
    You need to be careful when reading what admanb is saying, because some of the stuff he is talking about isn't in C++ syntax but rather is referring to the concept of having a matrix stored in a one dimensional array rather than a multidimensional array.

    Multidimensional arrays in C++ are arrays of arrays, and it is possible to make them such that they have varying length underlying arrays. If you want to just store a rectangular matrix of information that is a constant size then doing the one dimensional array trick is often a better idea so it is all in one contiguous block of memory. However, multidimensional arrays proper can still be useful or necessary, so I'd recommend going over the syntax in that link or other references and ask specific questions from there if need be.

    Savant on
  • SkyEyeSkyEye Registered User regular
    edited April 2010
    Also, I'm assuming that's a pass by reference, right?

    SkyEye on
    Steam: Autumn_Thunder - SC2: AutumnThundr.563 (NA) - Hearthstone: AutumnThundr.1383

  • admanbadmanb unionize your workplace Seattle, WARegistered User regular
    edited April 2010
    SkyEye wrote: »
    I need to know where you found that; I looked through at least a dozen forums and articles and got zip.

    Here.

    admanb on
  • SkyEyeSkyEye Registered User regular
    edited April 2010
    Alright guys, thanks. You've been a big help.

    SkyEye on
    Steam: Autumn_Thunder - SC2: AutumnThundr.563 (NA) - Hearthstone: AutumnThundr.1383

Sign In or Register to comment.