Using a Pointer for Holding Map Data?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

Using a Pointer for Holding Map Data?

Post 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?
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Using a Pointer for Holding Map Data?

Post 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
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: Using a Pointer for Holding Map Data?

Post 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.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Using a Pointer for Holding Map Data?

Post 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.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: Using a Pointer for Holding Map Data?

Post 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. ;)
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
Post Reply