Thread: Hello all and pointers to multi-dimensional arrays

  1. #1
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317

    Question Hello all and pointers to multi-dimensional arrays

    First and foremost, Hi
    I'm new here and since only recently I started to learn C++, I think I'll stick around

    I've been following the excellent tutorial here ate cprogramming as part of my learning, changing a lot of code to better see if I really understood each subject. On one of those changes I got stalled.

    Lesson 8 teaches arrays. Nothing wrong here, only when I was doing my own experiments, I applied what I had learned in lesson 6 - Pointers. Great lesson, btw! I finally was able to understand the buggers. Anyway... I tried to pass a pointer into a multidimesional array. Like this

    Code:
        char mycharr[8][8];
        char *ptrarr;
        ptrarr = mycharr;
    And it didn't work. I get a compile error like this:
    Code:
    assignment to `char *' from `char (*)[8]'
    It works fine for normal arrays, but I still couldn't figure it out how to use it on multi-dimensional ones even after messing with the above code.

    I use mingW that comes with Blodsheed's Dev-C++ IDE. Don't ask me compiler versions... I'm not even near that stuff
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I believe when you use a pointer it will be treated as a 1-dimensional array, even if you point it at a 2-dimensional.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    Maybe if you made teh pointer a 2D Array like so:

    Code:
        char mycharr[8][8];
        char **ptrarr;
        ptrarr = mycharr;
    (i think thats how u do it, im not positive though)
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Okiesmokie
    Maybe if you made teh pointer a 2D Array like so:

    Code:
        char mycharr[8][8];
        char **ptrarr;
        ptrarr = mycharr;
    (i think thats how u do it, im not positive though)
    I doubt it, since you wouldn't know if the array you point at is a 8x8, 4x16 or 2x32 array. This would cause trouble when indexing.

    A possible solution would be:
    char* ptrarr[8];

    Or simply treat the 2D array as a 1D array:
    char* ptrarr;
    ptrarr[Y * 8 + X] = 'A';
    Var = ptrarr[y * 8 + X];
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Originally posted by Magos

    Or simply treat the 2D array as a 1D array:
    char* ptrarr;
    ptrarr[Y * 8 + X] = 'A';
    Var = ptrarr[y * 8 + X];
    Err... I'm not sure I understood that code. I mean the part about placing the * immediatly after the keyword.
    Nevertheless the problem here is that the compiler gives an error not when trying to use the pointer, but before that, when defining it. That is,
    Code:
    char mycharr[8][8];
    char *ptrarr;                //compiles ok... obviously :cool: 
    ptrarr = mycharr;         //complains here
    Last edited by Mario; 05-15-2002 at 09:17 AM.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Mario

    Err... I'm not sure I understood that code. I mean the part about placing the * immediatly after the keyword.
    This is just a style issue. Both of the following statements are the same:

    int* pInt = NULL;

    int *pInt = NULL;

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Hmm, I made some tests on this one and I realized that some of my solutions didn't work so well. This is an example that works however:
    Code:
    int main()
    {
       char mycharr[8][8]={0};
       mycharr[0][0]='A';
       mycharr[0][2]='B';
       mycharr[2][0]='C';
       mycharr[3][5]='D';
    
       char *ptrarr;
       ptrarr = *mycharr;
    
       printf("%c %c %c %c", ptrarr[0*8+0], ptrarr[0*8+2], ptrarr[2*8+0], ptrarr[3*8+5]);
       getch();
       return 0;
    }
    Since mycharr is an array of arrays, you must set the pointer ptrarr like this:
    ptrarr = *mycharr;
    (The adress of the first array)
    instead of what I said in the previous post:
    ptrarr = mycharr;
    (The adress of the array of arrays)
    The above example also shows how you can simulate a 2D array using a 1D array (using X*Width+Y, or X*8+Y).
    Hope I was to some help .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    hmm what about this:

    Code:
        char mycharr[8][8];
        char **ptrarr;
        ptrarr = new char[8][8];
        ptrarr = mycharr;
    ??
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  9. #9
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Originally posted by Magos
    Code:
    int main()
    {
       char mycharr[8][8]={0};
       mycharr[0][0]='A';
       mycharr[0][2]='B';
       mycharr[2][0]='C';
       mycharr[3][5]='D';
    
       char *ptrarr;
       ptrarr = *mycharr;
    
       printf("%c %c %c %c", ptrarr[0*8+0], ptrarr[0*8+2], ptrarr[2*8+0], ptrarr[3*8+5]);
       getch();
       return 0;
    }
    [SNIP]
    The above example also shows how you can simulate a 2D array using a 1D array (using X*Width+Y, or X*8+Y).
    Hope I was to some help . [/B]
    Oh, Yes you were!
    It compile nicely and also helped me out understand it better. Thanks a lot
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Okiesmokie
    hmm what about this:

    Code:
        char mycharr[8][8];
        char **ptrarr;
        ptrarr = new char[8][8];
        ptrarr = mycharr;
    ??
    Sorry to dissapoint you, but:
    Code:
    int main()
    {
       char mycharr[8][8];
       char **ptrarr;
       ptrarr = new char[8][8];    //Line 8
       ptrarr = mycharr;           //Line 9
       getch();
       delete[] ptrarr;
       return 0;
    }
    
    Error:  pointer01.cpp(8,12):Cannot convert 'char ( *)[8]' to 'char * *'
    Error:  pointer01.cpp(9,12):Cannot convert 'char ( *)[8]' to 'char * *'
    Perhaps you should try out your code before you post it? I know I should
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. See the malloc thread on the C board at the moment
    http://www.cprogramming.com/cboard/s...threadid=17742

    2. Read this
    http://pw2.netcom.com/~tjensen/ptr/cpoint.htm

    3. The correct way to point at a 2D array, in such a way as to preserve all the [][] notation is like so
    Code:
    int a[10][20];
    int (*pa)[20] = a;

  12. #12
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    whoa! that's an helluva of information there.
    I didn't realize the question had such implications... too much info for this poor ol' noob

    Anyway, I'll sure take a close look at that tutorial. 12 pages on pointers just can't be wrong

    I'll also remember to search for answers also in C when getting stalled.

    Thanks
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Re: Hello all and pointers to multi-dimensional arrays

    [I tried to pass a pointer into a multidimesional array. Like this

    Code:
        char mycharr[8][8];
        char *ptrarr;
        ptrarr = mycharr;
    I think /.....
    Code:
        char mycharr[8][8];
        char *ptrarr;
        ptrarr = mycharr; // here you need to overload " = "
    Does it needs some overloading...?
    C++
    The best

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No, it just needs the pointer type fixing
    Get the type of pointer right, and it all falls into place

  15. #15
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Did it compile at the end....

    Did it compile... at the end...
    C++
    The best

Popular pages Recent additions subscribe to a feed