Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] pointer pointers in C#?

Started by avianRR Feb 17, 2007 at 11:18 AM 2 replies 750+ views
Original Post
avianRR
avianRR
Ok, I'm trying to convert some old C++ projects to C# and I'm having a little problem trying to figure out a good way to do a couple of things. For this post I'm gonna ask how to do pointer pointers as in the following code. struct GState{ int mode; bool active; // more stuff... } int nGridTiles = GridWidth * GridHeight; GState **StateGrid = new GState*[nGridTiles]; memset(StateGrid,0,sizeof(GState*) * nGridTiles); // NULL all pointers Since I don't know the size of the grid till runtime I don't know the size of the array, how can I do this? P.S. sorry for asking somthing that's probably gonna be a really slap my forhead cause I'm stupid for not seeing it question... AGAIN!
------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]
jfclavette
jfclavette
If you usually pass it by reference/pointer I would just convert it to a class.

public class GState{    int mode;    bool active;    // more stuff...}...int nGridTiles = GridWidth * GridHeight;GState[] StateGrid = new GState[nGridTiles];
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
Scet
Scet
If you need to be able to reference these tiles as objects, it would be better to make them into a class instead of a struct.

Then just make a simple 2D array:

GState[,] StateGrid = new GState[GridHeight,GridWidth];

You'll need to go though and initialzie each one though as this only creates an array of null GState objects.

for( int y = 0; y < GridHeight; y++ ){   for( int x = 0; x < GridWidth; x++ )   {       StateGrid[y,x] = new GState( /*pass parameters here*/ );   }}
avianRR
avianRR
Excelent, that'll work. And I did slap my forhead for feeling stupid. I've never used sytex like that in C++ but I have in other languages so it makes sense now. Both of the explanations also point out a couple of things that'll probably help with the next problem I was gonna have too.
If it initializes to null then I won't have to worry about that. Making it a class, not a problem. I like the 2D array though, didn't know that was possable like that.
Again, Thanks.
------------------------------Piggies, I need more piggies![pig][pig][pig][pig][pig][pig]------------------------------Do not invoke the wrath of the Irken elite. [flaming]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.