CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2009
    Posts
    58

    multi dimensional array

    Say we have this:
    int array[2]={1,2};
    int *ptr = array;
    cout<<ptr[1];


    Now if we have this
    int array[2][2];

    //pretend it contains values, save me writing code:0

    how do i assign a pointer to this multi dimensional array?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: multi dimensional array

    If you want a pointer to an array of 2 elements:
    Code:
    int (*p)[2] = array;
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2009
    Posts
    58

    Re: multi dimensional array

    I want a pointer to the multi dimensional array.

    Was thinking i need to use a double pointer for it. But just cant seem to implement it.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: multi dimensional array

    Quote Originally Posted by newbie30
    I want a pointer to the multi dimensional array.
    Why would you want that? You more likely want either a pointer to an array of 2 elements so that you can iterate over the elements of the 2D array, or a pointer to an int so that you can directly iterate over the ints in the 2D array.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Mar 2009
    Posts
    58

    Re: multi dimensional array

    With a one dimensional array we can have a pointer to it, correct?

    but with multidimensional you cant?

    I was just playing around with code and i was just trying to print the elements of the multidimensional array using a pointer, the same way you can with a one dimensional array.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: multi dimensional array

    Quote Originally Posted by newbie30
    With a one dimensional array we can have a pointer to it, correct?
    Yes, e.g.,
    Code:
    int numbers[2] = {1, 2};
    int (*p)[2] = &numbers;
    Quote Originally Posted by newbie30
    but with multidimensional you cant?
    No, you also can:
    Code:
    int numbers[2][2] = {{1, 2}, {3, 4}};
    int (*p)[2][2] = &numbers;
    Notice that I am taking you literally here: we are talking about pointer to arrays, not pointers to the first elements of arrays, hence I write &numbers on the right hand side of the assignment rather than just numbers to get the address of the array.

    Quote Originally Posted by newbie30
    I was just playing around with code and i was just trying to print the elements of the multidimensional array using a pointer, the same way you can with a one dimensional array.
    You can try:
    Code:
    #include <iostream>
    #include <cstddef>
    
    int main()
    {
        int numbers[2][2] = {{1, 2}, {3, 4}};
        const std::size_t total_size = sizeof(numbers) / sizeof(numbers[0][0]);
    
        for (int* p = &numbers[0][0], *end = &numbers[0][0] + total_size; p != end; ++p)
        {
            std::cout << *p << ' ';
        }
        std::cout << std::endl;
    }
    This would be the "pointer to an int so that you can directly iterate over the ints in the 2D array" idea.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jan 2009
    Posts
    19

    Re: multi dimensional array

    int myArray={{0.1} , {2,3} }
    Int (*ptr) [2][2] = &myArray;
    Last edited by Abhishek Chauhan; March 26th, 2009 at 02:17 AM.

  8. #8
    Join Date
    Mar 2009
    Posts
    58

    Re: multi dimensional array

    int(*ptr)[2][2] = &array;


    I know we are pointing to the address of the array but what does the first part actually say:

    ie this part int(*ptr)[2][2]


    Thanks

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: multi dimensional array

    Quote Originally Posted by newbie30
    I know we are pointing to the address of the array but what does the first part actually say:

    ie this part int(*ptr)[2][2]
    That means "a pointer named ptr to an array of two arrays of two ints".
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Mar 2009
    Posts
    58

    Re: multi dimensional array

    but why the brackets around the pointer?

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: multi dimensional array

    Quote Originally Posted by newbie30
    but why the brackets around the pointer?
    Ugly syntax inherited from C

    Without the parentheses you end up with an array of 2 arrays of 2 int pointers.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Mar 2009
    Posts
    58

    Re: multi dimensional array

    thanks the perfect answer for me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured