🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

pointer trouble

Started by
9 comments, last by fenrir 24 years, 6 months ago
int a[2] = {2,2}; int *pa = a; this works, but I need something like that for : int b[2][2] = {2,2,2,2}; How would I declare pb so I can do pb = b ? I tried int * pb; int * * pb; int * pb[]; but nothing works. Can anyone help me?
Advertisement
I''m sorry, you can''t do it the way you''re doing it. Derefencing a pointer to a pointer requires more information to the compiler. Specifically, it needs the size of the lower array. I have no idea if this will work, but it''s worth a shot!
int b[2][2] = {{2,2},{2,2}};int (**pb)[2][2]; 

In all honesty, I don''t see why this sort of code is truly necessary. There are much cleaner ways of writing the same thing.
Nah didn''t work.
Thanks anyway
Try:
int b[3][2];
int (*pb)[2];
pb = b;

This seemed to work OK for me but you might want to check it out thoroughly!
I agree with foofightr though, there has to be a cleaner way somewhere up the chain to do the same thing
-ns


-ns-
Great that works, thanx alot.

As for why I do this
I need to change the values of the array A, based on the current values in A for every frame, but when calculating the new values, the already changed values could affect the calculations.
ex. I change a[0][0] first
and then a[0][1] using a[0][0], but it should be the old value of a[0][0], not the new one.
So I store the new values in B.
The reason I want pa and pb is that I want pa always to point to the last updated array, so my algorithme always puts the new values in pb. After the calculations I just switch pa and pb so pa point to the newwest array.

If anyone can think of a better way to do this, please tell me
I might just make a big array:
int MasterArray[2][X][Y]; and keep an int around to determine which one is the old and which is the new. On the other hand, I might not since the pointers (I could be wrong about this) are probably faster, so it would depend on the size and processing method of this array. I also tend to shun multidimensional arrays in favor of doing the indexing myself. Then you can use lookup tables if you need even more speed.
Since you wrote the program you probably have a better idea of the best method than I do, but this *would* let you get past using those really weird looking pointers
-ns


-ns-
Why not:

struct data{
int a[10];
int b[10];
int temp[10];
int aIsCurrent; //boolean flag
};

It''s halfway to C++
-the logistical one-http://members.bellatlantic.net/~olsongt
I don''t really see why using these pointers would be ''ugly''.
After all I just have to swap them after my calculations, and all through the rest of my code I can use pa to point to the newest array, seems al lot easier than using an int to see which is newer.
That is just my idee ofcourse
I stand corrected then,
if it makes your code simpler, easier to read in the long run, easier to modify later, and possibly faster then it''s got to be the best solution for you.
In general though, if I have to check the MSDN Library and then make an educated guess to figure out how to construct something in C, its often a sign to me that I''m doing something suboptimally (not always though)
Good luck on the project.
-ns
-ns-
Nightshade''s method above is the only way you''ll be able to get the pointer to work like your trying -- c needs to know the size of all of the dimensions of an array - 1. This is the only way it can calculate offsets.

One other way you could do it is embed the info in a structure:

typedef struct {
int ary[2][2]
/*
Other state data here?
*/
} TWODARRAY, *PTWODARRAY;

TWODARRAY a;
TWODARRAY b;
PTWODARRAY pCur;
PTWODARRAY pOld;

pCur->ary[0][0] = pOld->ary[0][0];

It also lets you do cheesy stuff like:

//
// Copy the whole array.
//
*pCur = *pOld;

And return an array from a function.

If its the only data member you have, it is probably a bit kludgy to do it this way, but if you have other info to keep, I think packaging it up in this manner is probably best (at least in c..)

Notwenwww.xbox.com

This topic is closed to new replies.

Advertisement