Page 1 of 1

Using a Pointer for Holding Map Data?

Posted: Tue Jun 08, 2010 9:09 pm
by ibly31
Okay, So I'm working on an iPhone App, and I need to load level data. I have the strtok code working, and it seperates the numbers, and uses atoi() to get them to integers. But what I want to do is store them in a multidimensional array, and I need that array to be a variable accessible by everything(a variable in the implementation file of my EAGLView.h).

I don't really understand the usage of pointers, but my file has a header defining the width and height of the map. So I can define the width and height of the array of ints before I populate it. Buut, how do I define it in the header file? int mapNums[][] ?? Or should I use a pointer? If I say int *mapNums[][], would it be an array of pointers? Thats not what I want, I just want a dynamic/static multidimensional array that starts off with undefined width and height and is populated with numbers later... Any help?

Re: Using a Pointer for Holding Map Data?

Posted: Tue Jun 08, 2010 9:16 pm
by avansc
you can do something like this

tile *map = (tile*)malloc(sizeof(tile)*width*heigh);

tile *it = map;

while(!eof or whatever)
{
read from the file
*it = whatever
it++;
}

then when you want to access the map data you can do map[width*y+x] // x and y being a tile location

Re: Using a Pointer for Holding Map Data?

Posted: Wed Jun 09, 2010 5:48 am
by RyanPridgeon
An array and a pointer can be used extremely similarly. For example, you can still use the [] indexing method for a pointer, and it will look along the memory pointed at by the pointer. For example,

Code: Select all

int someArray[3];
someArray[2] = 5;

int* somePointer;
somePointer = someArray;

printf("%d", someArray[2] ); // this prints the number 5
printf("%d", somePointer[2] ); // and so does this
The advantage of using a pointer over a static array is, of course, that it's not static. You can create any size array by allocating memory using malloc and pointing at it. Google malloc for more about that. This is what I recommend you do.

As for using your map pointer, If you're declaring the pointer in a header, it's best to use extern. I'm not sure why, but I've had problems in the past with declaring variables straight up in headers. For example, in the header;

Code: Select all

extern int *map;
Then in the source file have

Code: Select all

int *map
This will allow you to use the map array/pointer in any file that includes the header, yet it will still be declared in a source file.

Re: Using a Pointer for Holding Map Data?

Posted: Wed Jun 09, 2010 8:48 am
by Falco Girgis
He's wanting to utilize a MULTIDIMENSIONAL, dynamic array.

You would declare it as such:

Code: Select all

int **map;
That is a pointer to a pointer.

First, we make this pointer point to an array of pointers:

Code: Select all

map = (int **)malloc(sizeof(int *) * height);
We have just allocated the rows.

Now, we make each pointer in our array of pointers point to its own array (the columns):

Code: Select all

for(int i = 0; i < height; ++i) map[i] = (int *)malloc(sizeof(int) * width);
You can access this as with any 2D array:

Code: Select all

map[row][col] = whatever;

If you wish to be able to access this array as a global (throughout the project), you will want to do as Ryan says and declare it extern in its respective header file.
[quote="RyanPridgeon"]As for using your map pointer, If you're declaring the pointer in a header, it's best to use extern. I'm not sure why, but I've had problems in the past with declaring variables straight up in headers. For example, in the header;[/quote]It's because the linker bitches at you for duplicate symbols. Each file including that header now has its own instance of the object.

I wouldn't recommend just declaring the map array extern, though. Obviously it isn't very helpful. Create a Level class to encapsulate the width, height, tile array, and other attributes associated with the level. Then declare that extern.

Re: Using a Pointer for Holding Map Data?

Posted: Wed Jun 09, 2010 11:22 am
by RyanPridgeon
GyroVorbis wrote: I wouldn't recommend just declaring the map array extern, though. Obviously it isn't very helpful. Create a Level class to encapsulate the width, height, tile array, and other attributes associated with the level. Then declare that extern.
Yeah that's what I'd do too. However I thought he might be using C, so I went with the procedural way. ;)