passing array in a function

Apr 18, 2014 at 9:28pm
i have defined an object which is an array of my class called Player now i want to pass this whole array as a parameter in a function, i think it will be done by using pointers but i am not able to figure out how it is done?
1
2
Player player[4];
void showscoreboard("          ", int cround){}

" " represents the blank where the array should b passed.
Last edited on Apr 18, 2014 at 9:30pm
Apr 19, 2014 at 12:09am
Instead of a array, use STL's vector
http://www.cplusplus.com/reference/vector/vector/
Apr 19, 2014 at 1:07am
closed account (D80DSL3A)
Or..
void showscoreboard( Player[] playerArr, int cround){}
The size of the array should also be passed to the function. Perhaps that's what cround is?

call as:
showscoreboard( player, 4);
Apr 19, 2014 at 2:12am
Yep, agree! But vectors are always better :P
Apr 19, 2014 at 2:22am
closed account (D80DSL3A)
Certainly. Though I would say 'often' rather than 'always' myself.
In the present case, use of a vector removes the need to pass the array size (and may prove useful in other ways as well). Thought I'd answer as asked in case he is required to do it the way shown (probable, since he's been taught that way).
Apr 19, 2014 at 5:00am
@fun2code : actually this function 'showcoreboard' is going to show a scoreboard and for that it needs the round no and the marks of the players in that round
Apr 19, 2014 at 5:13am
i want to use it like this. please have a look....

1
2
3
4
5
6
7
void showscoreboard(Player[] cplayer,int size, int cround)
	{
		cout << "		|"<< cplayer[0].getname() <<     " |"      << cplayer[1].getname <<"|" <<      cplayer[2].getname <<"|"<< cplayer[3].getname << endl
			 << "round1 |        "<< cplayer[0].getscore[cround] << " | " <<cplayer[1].getscore[cround]<< " | " <<cplayer[2].getscore[cround]<< " | "<<cplayer[3].getscore[cround] << endl
			 << "round2 |        "<< cplayer[0].getscore[cround] << " | " <<cplayer[1].getscore[cround]<< " | " <<cplayer[2].getscore[cround]<< " | "<<cplayer[3].getscore[cround] << endl
			 << "round3 |        "<< cplayer[0].getscore[cround] << " | " <<cplayer[1].getscore[cround]<< " | " <<cplayer[2].getscore[cround]<< " | "<<cplayer[3].getscore[cround] << endl;
	}
Apr 19, 2014 at 11:39am
ok thanx...
Apr 21, 2014 at 11:02pm
Never pass an array. This is frowned up in C++ as it's very slow. Pass pointers instead- or use Vectors somehow to pass a pointer instead.

I would make an array, then declare a pointer to the array.

After this, much is highly dependent on if your program is in different files - i.e. different classes, or it's all in one big file.

If your program is in different files, then the definition of the array in the header file is to be included in the other source file: #include "ClassArray.h" somehow.

If your program is all in one file, then the #include is obviously not necessary.

example of Microsoft CArray declaration and using it in parameters:
in the .h file, the definition in a CEnum class:
CArray<CTgA::TRGTAPP, CTgA::TRGTAPP&> a_tga;
where CTgA is a (different) class, and TRGTAPP is a structure (top name) defined in said class in the Public: section.
1
2
3
4
5
6
7
8
struct TRGTAPP
	{
		HWND a_HWND;
		CWnd *p_CWndWin;
		CString a_Name;
                 .
                 .
                 .


a function prototype in a .h file passing the pointer to the array:
void fn_BuildArray(CWnd* c_obj, CArray<CTgA::TRGTAPP, CTgA::TRGTAPP&>& a_tga);

The call to the function in a .cpp file passing the CArray name:
this->fn_BuildArray(p_CWnd_lcl, CEnum::a_tga);

The actual function in a .cpp file:
void CEnum::fn_BuildArray(CWnd *p_CWnd, CArray<CTgA::TRGTAPP, CTgA::TRGTAPP&>& a_tga)

Because CArray in MFC is template-based, just passing 'the name' is not really passing the name, but passing a pointer instead.

hth
Last edited on Apr 22, 2014 at 6:07am
Apr 21, 2014 at 11:14pm
markj wrote:
Never pass an array. This is frowned up in C++ as it's very slow.

As it is actually impossible to pass an array in C++ (or C), it cannot be either fast or slow.

But, since array names degenerate into pointers, you wind up passing a pointer anyway, and it doesn't get much faster than that.
Topic archived. No new replies allowed.