Using Pointer Arithmetic on 2-Dimensional Arrays

Started by
8 comments, last by pizza box 22 years, 1 month ago
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
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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
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(**)?
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
calm down there sparky
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!
Thanks a person, I am using single indexing and it works fine now.
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
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.

This topic is closed to new replies.

Advertisement