|
-
March 25th, 2009, 12:13 PM
#1
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
-
March 25th, 2009, 12:26 PM
#2
Re: multi dimensional array
If you want a pointer to an array of 2 elements:
Code:
int (*p)[2] = array;
-
March 25th, 2009, 12:30 PM
#3
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.
-
March 25th, 2009, 12:32 PM
#4
Re: multi dimensional array
 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.
-
March 25th, 2009, 12:51 PM
#5
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.
-
March 25th, 2009, 01:06 PM
#6
Re: multi dimensional array
 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;
 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.
 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.
-
March 26th, 2009, 02:12 AM
#7
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.
-
March 26th, 2009, 10:30 AM
#8
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
-
March 26th, 2009, 10:59 AM
#9
Re: multi dimensional array
 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".
-
March 26th, 2009, 11:08 AM
#10
Re: multi dimensional array
but why the brackets around the pointer?
-
March 26th, 2009, 11:15 AM
#11
Re: multi dimensional array
 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.
-
March 26th, 2009, 11:26 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|