Skip to main content
gamedev.net
🔒 Locked

array parameters

Started by wannabean333 Jul 3, 2004 at 3:17 PM 5 replies 900+ views
Original Post
wannabean333
wannabean333
hey everyone- I know this may sound very, very noobish, but how do you use an array as a parameter in a function? (an integer array) like int draw_map(int map[WHAT_DO_I_PUT_HERE][AND_HERE])
Willbo
Willbo
You should be able to leave them empty or with a fixed number, otherwise you could just parse in some pointers.

int draw_map(int** map)

- Patrik Willbo
Willbo
Willbo
Noobish, nah. Just some encouragement I guess, but don't call yourself noobish, even thought your new to some stuff. But you can't just get the hang of everything in just a couple of seconds :D Have a great weekend wannabean333!

- Patrik Willbo
wannabean333
wannabean333
i love gamedev.net

so many people so fast

it's like having a computer science professor right next to me
wannabean333
wannabean333
OK here's my function prototype:
void Generate_Glow(int size, double intensity, int** alpha_map);

When I call my function like this:
Generate_Glow(64,256,glow_alpha);

glow_alpha is my [64][64] array for my alpha map.

when i try to compile this, it says:
cannot convert parameter 3 from 'int [64][64]' to 'int ** '

How do i solve this?

[Edited by - wannabean333 on July 3, 2004 5:38:56 PM]
bytecoder
bytecoder
You have to specify the last dimension of a multi-dimensional array, e.g:
void Generate_Glow(int size, double intensity, int alpha_map[][64]);
hplus0603
hplus0603
Any array, no matter what the dimension, is only single dimensional. An array of pointers is NOT the same thing as an array of arrays. When you say:

int foo[32][64];


it means that "foo is an array of 32 arrays of 64 ints". Thus, in memory, you'd get 64 ints, followed by 64 ints, followed by ... up to 32 times. When you pass "foo" to a function, it's passed as an int pointer. The array loses its dimensionality that way, so you have to pass the size separately, and index by doing your own multiplication:

int foo[32][64];void init_first_10( int * ptr, int stride ) {  for( int ix = 0; ix < 10; ++ix ) {    int * tmp = &ptr[stride*ix];    for( int iy = 0; iy < stride; ++iy ) {      tmp[iy] = 0;    }  }}int main() {  init_first_10( foo, 64 );  return 0;}


If you know for a fact what the dimensions are (for all dimensions except the first) then you can declare it like so:

int foo[32][64];void init_first_10( int ptr[][64] ) {  for( int ix = 0; ix < 10; ++ix ) {    for( int iy = 0; iy < 64; ++iy ) {      ptr[ix][iy] = 0;    }  }}int main() {  init_first_10( foo );  return 0;}

enum Bool { True, False, FileNotFound };

Topic Locked

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

Sign in to reply to this topic.