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

2D array of pointers

Started by BlackEye Oct 20, 2003 at 7:18 AM 2 replies 300+ views
Original Post
BlackEye
BlackEye
guys, I want to read in a text file containing a 2D grid of numbers. The txt file also contains the grid size. eg. Width 3 Height 3 1 3 6 3 5 8 9 5 2 I want to use a 2D array of char to hold this. So how do i declare my member variable? char** m_Grid; ?? then is it char tile[2]; for(int i = 0; i <= Height; i++) { for(int i = 0; i <= m_PitchTileWidth; i++) { fscanf(indexfile,"%s",tile); (WHAT GOES HERE!!? Is it m_Grid[Height] = new char(tile) } } Thanks for any help, bit puzzled, been a while!
dmounty
dmounty
In each element of your grid, you have to make an array of pointers to your object type.. ie


char** cGrid=new char*[20];

for (int x=0;x<20;x++) {
cGrid[x]=new char[15];
}


and then to delete it all:


for (int x=0;x<20;x++) {
delete[] cGrid[x];
}

delete[] cGrid;


hope that helps
BlackEye
BlackEye
A little!

But i need to use int not char, as my text file can contain two digit numbers, also its easier later if its in this format.
So i have made int** m_PitchTileGrid;

then my readin function is this...
Still not there yet !! Help!!!


cPitch::LoadPitchFile()
{

FILE* indexfile;
char line[30];
int tile;

if((indexfile = fopen("Data\\graphics\\pitch\\pitch.txt","r"))==NULL)
{
MessageBox(NULL,"Error : Unable to load Pitch Data File.",
"LoadBitmaps() Error", MB_OK);
}
fscanf(indexfile,"%s",line);
//m_kitsurfaces;

while (!feof(indexfile))
{

fscanf(indexfile,"%d",&m_PitchTileWidth);
fscanf(indexfile,"%d",&m_PitchTileHeight);
fscanf(indexfile,"%d",&m_PitchBorder);

m_PitchTileGrid = new int*[m_PitchTileHeight];

for(int i = 0; i <= m_PitchTileHeight; i++)
{
for(int j = 0; j <= m_PitchTileWidth; j++)
{
fscanf(indexfile,"%d",&tile);
m_PitchTileGrid[i][j] = new int;
m_PitchTileGrid[i][j] = tile;
}
}
}

fclose(indexfile);

}
BlackEye
BlackEye
Ok, i think i have it, is this right! it compiles, just the graphics are out of sync? Could be another function thats got a bug in it though,


fscanf(indexfile,"%d",&m_PitchTileWidth);
fscanf(indexfile,"%d",&m_PitchTileHeight);
fscanf(indexfile,"%d",&m_PitchBorder);

m_PitchTileGrid = new int*[m_PitchTileHeight];

for(int i = 0; i <= m_PitchTileHeight; i++)
{
m_PitchTileGrid[i] = new int[m_PitchTileWidth];

for(int j = 0; j <= m_PitchTileWidth; j++)
{
fscanf(indexfile,"%d",&tile);
//m_PitchTileGrid[i][j] = new int;

m_PitchTileGrid[i][j] = tile;
}
}


Thanks for you help

GOT IT !! JUST < NOT <= !! lol ! always gets me!!


[edited by - BlackEye on October 20, 2003 10:34:56 AM]

Topic Locked

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

Sign in to reply to this topic.