Using #define within a function to change a current definition

Hi. I'm new here to the c++ forums. I apologize in advance if this topic has already been discussed before, or if this is in the wrong section.

Basically, what I'm trying to do is change the value of a variable I could use as an int in the declaration of a function when using multidimensional arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#define HEIGHT 16
#define WIDTH 32
#include <iostream>
using namespace std;

void draw(int map[HEIGHT][WIDTH])
{
    for(int y=0;y<HEIGHT;Y++)
    {
        for(int x=0;x<WIDTH;x++)
        {
        cout << map[y][x];
        }

    }
}//end of func



int main()
{
int newwidth = 24;
#define WIDTH 24 //this is where I get an error
int map[16][newwidth]; // a 2d array used as a map
draw(map);

return 0;
}


but when i try to change the WIDTH variable within a function using #define, I get the error message "this is the location of the previous definition".

I'm probably defining the WIDTH the wrong way, please help.
but when i try to change the WIDTH variable within a function using #define, I get the error message "this is the location of the previous definition".

On my compiler (Visual Studio 2005), I simply get a "macro redefinition" warning. You'll probably have to change the warning level (if possible) on your compiler to downgrade it from a compilation error to a warning.

But, are you sure that's what you want to do?

It would mean up here:

1
2
3
4
5
6
7
8
9
10
11
void draw(int map[HEIGHT][WIDTH])
{
    for(int y=0;y<HEIGHT;Y++)
    {
        for(int x=0;x<WIDTH;x++)
        {
        cout << map[y][x];
        }

    }
}//end of func 


WIDTH would be 32, and down here:

int map[16][newwidth]; //assuming newwidth will be changed to WIDTH

WIDTH would be 24. That would mean walking off the end of the array in the call to draw(map);
maybe you should do #ifdef
I don't understand how #ifdef would work the way I'm looking for. I'm wanting to make the WIDTH variable available across multiple files and to be modifiable, instead of doing it the "long way" by making a global variable and using "extern int WIDTH;" in all of my source files. is there a way of modifying a global variable that i have previously declared with "#define" in a header file?

EDIT: I just realized you can't use non-constant ints to declare array bounds when passing to a function. so I'm looking for another way to make a modifiable variable I could use to pass different sizes of arrays with. I appreciate all the help.

EDIT: I'm using Code::Blocks as my IDE, and I'm using the MinGW compiler that was packaged with it.
Last edited on
#define will not do what you want here. Nor should you ever use it for this purpose.

"#define X Y" just replaces instances of X with Y. Nothing more.

Therefore, this code:
1
2
3
4
5
6
7
8
9
10
#define SIZE 10
void func(int map[SIZE][SIZE])
{}

#define SIZE 5  // assuming this worked...
int main()
{
  int foo[SIZE][SIZE];
  func(foo);
}


Would be exactly the same as this code:
1
2
3
4
5
6
7
8
void func(int map[10][10])  // replace SIZE with 10
{}

int main()
{
  int foo[5][5];  // replace SIZE with 5
  func(foo);
}


Of course if you try this, you'll see it won't work because you're trying to pass a [5] width array as a [10] width array.


There are many reasons to avoid #defines. For numerical constants, you're better off with a constant:

 
const int size = 10; // much better than #define SIZE 10 



Anyway... as for your problem.... multidimentional arrays make this difficult. They're syntax poison. Read this article for more info and some ways you can work things out:

http://cplusplus.com/forum/articles/17108/
After following your help and reading the article, I've fixed my mapdraw function...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void mapdraw(int map[], int Width, int Height) // draws the map to the console
{
	for(int y=0;y<Height;y++) //sets the y coords
	{
		for(int x=0;x<Width;x++) //sets the x coords
		{
			map[(y*Width) + x] = x; //sets the tile int for the map
			cout << map[(y*Width) + x];
			if(x<9) cout << "|";
		}
		cout << endl;
	}

}

int main()
{
	int map[100]; //inits the map array 10 X 10 or 100

	mapdraw(map, 10, 10); //draws the map to the console

	cin.get(); //input delay, so i can read the console
	return 0;
}


My Console Output:

0|1|2|3|4|5|6|7|8|9
0|1|2|3|4|5|6|7|8|9
0|1|2|3|4|5|6|7|8|9
0|1|2|3|4|5|6|7|8|9
0|1|2|3|4|5|6|7|8|9
0|1|2|3|4|5|6|7|8|9
0|1|2|3|4|5|6|7|8|9
0|1|2|3|4|5|6|7|8|9
0|1|2|3|4|5|6|7|8|9
0|1|2|3|4|5|6|7|8|9



As for what I'm trying to do, it works perfectly. thanks. there is 1 issue, whenever I want to make a map to use with this mapdraw function, I will have to declare it in 1D, I can easily do this by multiplying the desired Width and Height, but I can handle that, just as long as my maps don't get too big(there is a limit in the maximum size of an integer, right?).

This is the first time I've ever went to a forum for help on any topic, and you helped me well, thanks.
Last edited on
Topic archived. No new replies allowed.