Passing a structure array in a function

Hi all,

I have been struggling with this for hours....

I have defined a structure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct unit {
		ALLEGRO_BITMAP *pic;
		char *name;
		int strength, hp, initiative;} ground_unit[4];

	ground_unit[1].name="Abrams";
	ground_unit[1].strength=150;
	ground_unit[1].hp=1000;
	ground_unit[1].initiative=2;
	ground_unit[2].name="Legionnaire";
	ground_unit[2].strength=50;
	ground_unit[2].hp=500;
	ground_unit[2].initiative=5;
	ground_unit[3].name="Flower";
	ground_unit[3].strength=250;
	ground_unit[3].hp=3000;
	ground_unit[3].initiative=1;


two variables:
 
int l=0, m=1;


and trying to set up the following function....

1
2
3
4
5
void draw_all(int &l, int &m, struct unit ground_unit[4]){
						al_draw_textf(font1, al_map_rgb(0,255,255), 220, 220,0, "Unit: %s", ground_unit[l].name); 
						al_draw_textf(font1, al_map_rgb(0,255,255), 420, 220,0, "Unit: %s", ground_unit[m].name); 

}


I know that you may not be familiar with allegro, but my problem is purely in c/c++ and is about the two last lines.

So basically I want the function to draw "ground_unit[0].name" and "ground_unit[1].name". However I am completely lost, I have tried the following combinations:


1
2
3
4
5
void draw_all(int l, int m, struct unit ground_unit[4]){
						al_draw_textf(font1, al_map_rgb(0,255,255), 220, 220,0, "Unit: %s", ground_unit[l].name); 
						al_draw_textf(font1, al_map_rgb(0,255,255), 420, 220,0, "Unit: %s", ground_unit[m].name); 

}


1
2
3
4
5
void draw_all(struct unit ground_unit[4]){
						al_draw_textf(font1, al_map_rgb(0,255,255), 220, 220,0, "Unit: %s", ground_unit[&l].name); 
						al_draw_textf(font1, al_map_rgb(0,255,255), 420, 220,0, "Unit: %s", ground_unit[&m].name); 

}


1
2
3
4
5
void draw_all(int &l, int &m, struct unit ground_unit[4]){
						al_draw_textf(font1, al_map_rgb(0,255,255), 220, 220,0, "Unit: %s", ground_unit[&l].name); 
						al_draw_textf(font1, al_map_rgb(0,255,255), 420, 220,0, "Unit: %s", ground_unit[&m].name); 

}


1
2
3
4
5
void draw_all(int &l, int &m, struct unit ground_unit[4].name){
						al_draw_textf(font1, al_map_rgb(0,255,255), 220, 220,0, "Unit: %s", ground_unit[l].name); 
						al_draw_textf(font1, al_map_rgb(0,255,255), 420, 220,0, "Unit: %s", ground_unit[m].name); 

}


1
2
3
4
5
void draw_all(int &l, int &m, struct unit char ground_unit[4].name){
						al_draw_textf(font1, al_map_rgb(0,255,255), 220, 220,0, "Unit: %s", ground_unit[l].name); 
						al_draw_textf(font1, al_map_rgb(0,255,255), 420, 220,0, "Unit: %s", ground_unit[m].name); 

}


None are working....any insight would be very helpful....

For the first try I get:

1>c:\users\wboustany\desktop\project_allegro\pic display test\pic display test\main.cpp(23): error C2036: 'unit []' : unknown size
1>c:\users\wboustany\desktop\project_allegro\pic display test\pic display test\main.cpp(23): error C2027: use of undefined type 'unit'
1> c:\users\wboustany\desktop\project_allegro\pic display test\pic display test\main.cpp(19) : see declaration of 'unit'
1>c:\users\wboustany\desktop\project_allegro\pic display test\pic display test\main.cpp(23): error C2228: left of '.name' must have class/struct/union
1>c:\users\wboustany\desktop\project_allegro\pic display test\pic display test\main.cpp(24): error C2036: 'unit []' : unknown size
1>c:\users\wboustany\desktop\project_allegro\pic display test\pic display test\main.cpp(24): error C2027: use of undefined type 'unit'
1> c:\users\wboustany\desktop\project_allegro\pic display test\pic display test\main.cpp(19) : see declaration of 'unit'
1>c:\users\wboustany\desktop\project_allegro\pic display test\pic display test\main.cpp(24): error C2228: left of '.name' must have class/struct/union

Please help and thanks,

Wissam
Last edited on
Pass the structure by reference. Prototype of draw_all should be
void draw_all(int l, int m, struct unit *ground_unit)
{
.....
}

Now do whatever you need to do in the function.
It should work (the first ones).
Look at this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

struct str{
    std::string s;
} array[] = {"hello", "world"};

void print(int a, int b, struct str arr[]){
    std::cout << arr[a].s << " " << arr[b].s;
}

int main(){
    print(0, 1, array);
    std::cin.get();
    return 0;
}


Compile this. It should work. Then try to find the differences.
By the way, could it be that you didn't define unit before the function, only forward declared it?
Last edited on
Thank you very much guys. Hamsterman I have built on your example and now it works !

However this spanned several questions important for my understanding.

If I put the structure globally (and not in the main loop as before) it does work well....
1
2
3
4
5
6
7
8
9
10
11
12

	struct unit {
	ALLEGRO_BITMAP *pic;
	char *name;
	int strength, hp, initiative;} ground_unit[4];

void draw_all(int l, int m, ALLEGRO_FONT *font1, struct unit ground_unit[]){
						al_draw_textf(font1, al_map_rgb(0,255,255), 220, 220,0, "Unit: %s", ground_unit[l].name); 
						al_draw_textf(font1, al_map_rgb(0,255,255), 420, 220,0, "Unit: %s", ground_unit[m].name);  }

int main(int argc, char *argv[]) { blablabla


does not show any error!

However this does:
1
2
3
4
5
6
7
8
9
10
11
12

void draw_all(int l, int m, ALLEGRO_FONT *font1, struct unit ground_unit[]){
						al_draw_textf(font1, al_map_rgb(0,255,255), 220, 220,0, "Unit: %s", ground_unit[l].name); 
						al_draw_textf(font1, al_map_rgb(0,255,255), 420, 220,0, "Unit: %s", ground_unit[m].name);  }

	struct unit {
	ALLEGRO_BITMAP *pic;
	char *name;
	int strength, hp, initiative;} ground_unit[4];

int main(int argc, char *argv[]) { blablabla


why ?

also, is there a way to declare the structure locally and still be able to use it in a function ?

Many thanks for your help guys,

Regards,

Wissam

Last edited on
You have to define stuff before you use it. That way the compiler only has to read your code once thus saving compilation time. Note: read this (4th, 5th sections) a bit http://www.cplusplus.com/forum/articles/10627/ to know forward declaring.

You can declare functions localy, but then you'll only be able to use them in that scope.
Topic archived. No new replies allowed.