Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

pizza box

Using Pointer Arithmetic on 2-Dimensional Arrays

This topic is 6141 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In the program I am creating, I convert an array into a pointer and then use pointer arithmetic to index it. Example: a[3] *( a + 3 ) My question is how would I do this on a 2-dimensional array? Example: a[3][2] ?? Not sure how to implement this

Share this post


Link to post
Share on other sites
Advertisement
Question : have you tried ?

If you have int a[10][10], then a is an int** pointing at the rows, and you can do pointer arithmetic on it. Similarly *a is an int* pointing at the elements, and you can do pointer arithmetic on it.

Share this post


Link to post
Share on other sites
quote:
Original post by Sethius
In the program I am creating, I convert an array into a pointer and then use pointer arithmetic to index it.
...
My question is how would I do this on a 2-dimensional array?

My question is why? Array subscript is pointer notation. Try this in your compiler:
// x[3]
3[x];


Anyway, *((*(a + 3)) + 2). Which is so ugly and convoluted that I can't understand why you'd bother.

[Edit: I never realized the board had a quick hyperlink tag]

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!


Edited by - Oluseyi on February 22, 2002 10:58:33 PM

Share this post


Link to post
Share on other sites
Ok, if there is a way around pointer arithmetic I''ll use it, but I''m stuck at this one point. I have to return a 2d-array from an accessor function ( its a private member of a class ), and then send that array to another function. What would I return the array as? I converted it to a double pointer (**) and then sent that to the other function, but whenever I tried to index it, the program would crash. Is there a better way to return an array than to convert it to a pointer?

One more question. I have a function that looks similar to this

// Returns a pointer to the dimensions
int** CBlock::GetDimensions( void )
{
return dimensions;
}

Where dimensions is a [4][2] array. When compiled, I receive this error:

error C2440: ''return'' : cannot convert from ''int [4][2]'' to ''int ** ''

Isn''t a 2d-array([][]) the same as a double pointer(**)?

Share this post


Link to post
Share on other sites
why not the normal method of accessing anything thats stored on a block. ie

ptr[x+y*width]
or
*(ptr+x*width) // same thing as above

EDIT: removed highly harsh comments that seem to offend some ppl due to a misuncderstanding of what consitutes a 2d array. maybe ppl should start calling the video buffer varity 2d linear arrays.

Edited by - a person on February 25, 2002 3:05:02 AM

Share this post


Link to post
Share on other sites
quote:
Original post by a person
i dont understand HOW you ppl are so ignorent of this.
...
i am amazed that you ppl (repliers and sethius) have not even bothered to learn the fundamentals of arrays and pointers before you started creating games.

Shove a sock in it.

There are different levels of indirection between 1- and 2-dimensional arrays. You can access pretty much anything as anything else with casting. Yes, it''s all linear memory (in protected mode) and yes, my last answer was a crock. Couldn''t be bothered to come back to it. operator[] adds the offset to the base and returns the dereferenced value, unless overloaded.

a[2][3] and a[2*3] (ie a[6]) are not the same. Why? Because a[0][2] and a[1][0] are not guaranteed to be next to each other as a[3] and a[4] are.

Does everybody feel better now?

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
quote:
Original post by a person
I am amazed that you ppl (repliers and sethius) have not even bothered to learn the fundamentals of arrays and pointers before you started creating games.

maybe this is why ppl have some much trouble with dyanmic memory, arrays, pixel access in a video buffer.



Have I ever said I was making games ? Or having any problem ?

A 2D T array is a T**, the converse is not true. RTFM.

K&R (2nd) 5.7-5.9
Stroustrup (3rd) C.7

Share this post


Link to post
Share on other sites
i may have been a bit rough, and was wrong about how the [][] notation works (i never use it). maybe because i have never seen a good use for using static arrays of arrays and seeing MANY questions and ppl refering to 2d arrays when speaking of video buffers. unlike most, i am self taught c/c++/asm coder from the help files. i learned most of my c stuff from the bordland turbo c help docs (gold ole f1). though later with internet access i was able to learn dx, c++, win32 api quite quickly.

i have erased my harsh statments, since they probally were a bit too harsh. (never post when frusterated/angry, it never bodes well). i also tend to see things differently then most ppl, since i tend to be able to visulize how the code should work to do certain tasks. i say no reason why he should be using an array of arrays when a simple array would suffice.

when using arrays of arrays i tend to allocate the memory myself and not deal with using [][] notation even for access (ussually only use char** or CSomeClass**).

i think next time the poster should make it clearer what is trying to be done vs ysing ambigous terms (yes 2d arrays is ambigous since both you and I saw it as two different meanings).

please tell me how to refer to a 2 dimensional block of memory accessed via a linear buffer? since calling 2d array is not correct. btw this is not being sarcastic (well slightly), since i truly am curious to what they should be called? a linear 2d array perhaps? if so, please add this to the faq/dictionary, i am sure many would appreciate knowing this so nobody has this misunderstanding again.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!