Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++

Started by clrscr Jan 18, 2002 at 2:51 PM 7 replies 500+ views
Original Post
clrscr
clrscr
How do I pass an array to a function. I tried this:
  
void Cgraphics::changeAmbient(float *new_values)
{
	for(int i = 0;i<sizeof(ambientlight);i++)
	{
		ambientlight[i]=new_values[i];
	}

}
  
but but that crashed my computer. What am I doing wrong? "A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila." -Mitch Ratliffe
-----------------------------When men speak of the future, the Gods laugh.An apology for the devil: it must be remembered that we have heard one side of the case. God has written all the books.Samuel Butler (1835 - 1902)
GBGames
GBGames
well that is the actual function.

How do you pass the array?

If you have:

Array[9]; //ten entry array

then you do:

changeAmbient(Array);

It should work.
-------------------------GBGames' Blog: An Indie Game Developer's Somewhat Interesting ThoughtsStaff Reviewer for Game Tunnel
clrscr
clrscr
I have this:
  
float light_a[] = { 0.5f, 0.5f, 0.5f, 1.0f };
changeAmbient(light_a);

and it crashes

"A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila."
-Mitch Ratliffe
-----------------------------When men speak of the future, the Gods laugh.An apology for the devil: it must be remembered that we have heard one side of the case. God has written all the books.Samuel Butler (1835 - 1902)
GBGames
GBGames
Well instead of
void Cgraphics::changeAmbient(float *new_values)

how about
void Cgraphics::changeAmbient(float new_values[])

It shouldn''t make too much of a difference, but hey it is worth a shot.

Also what compiler are you using?
-------------------------GBGames' Blog: An Indie Game Developer's Somewhat Interesting ThoughtsStaff Reviewer for Game Tunnel
Oluseyi
Oluseyi
  void Cgraphics::changeAmbient(float *new_values, int num_values)
{
for(int i = 0; i < num_values; ++i)
{
ambientlight[i] = new_values[i];
}
}

Pass in the size of the array. Also, usewhitespacetoenhancereadability (decipher that and learn a valuable lesson!)

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
clrscr
clrscr
That fixed my problem, thank you, but what is the use of
sizeof() then?

"A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila."
-Mitch Ratliffe
-----------------------------When men speak of the future, the Gods laugh.An apology for the devil: it must be remembered that we have heard one side of the case. God has written all the books.Samuel Butler (1835 - 1902)
jassmith
jassmith
sizeof() is useful whenever you need to know the number of bytes a variable is. For example, when using memcpy(), you pass in a source and destination pointer along with the number of bytes to copy. Sometimes structs have a size field which you need to initialize with the size of the struct.

Jason
Oluseyi
Oluseyi
The limitation to using sizeof is that it cannot determine the size of dynamic structures. Observe:
int *iArray; // iArray is a dynamic structure since its
// size is not explicitly declared
iArray = new int[20]; // allocate 20 integers = 320 bytes (Win32)
cout << sizeof(iArray) << endl;

Program output is 4. Why? Because iArray is a pointer to memory, not thee actual memory itself. Therefore, sizeof returns the size of the pointer which is 4 bytes (32 bits) on Win32 systems. That was your problem with sizeof(ambientlight).

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Topic Locked

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

Sign in to reply to this topic.