Skip to main content
gamedev.net
🔒 Locked

Passing array into a function

Started by pteri498 13 replies 1.6k views
Original Post
pteri498
pteri498
Here's what I'm trying to do: I have a character array called "keys[256]". I'm trying to pass that into a function so that a DirectInput 8 Device can do its work on that array. So here's basically what I've set up for that particular function:

void di8::getKeyboardState(char buffer[])
{
	pdiDevice->GetDeviceState( sizeof(&buffer),(void*)&buffer);
};

//function call
input->getKeyboardState(keys);

Only the function appears to have no affect on the members of the array that I pass into this function. I did a breakpoint to see what it looked like, and "buffer" here, and the only thing I see there is the number 0. Nothing that looks remotely like an array. Just a zero. Anyone see a way to get this function to do what I want?
Deniz
Deniz
A variable of type "array" is a kind of pointer. So on this line:
Quote:
pdiDevice->GetDeviceState( sizeof(&buffer),(void*)&buffer);

you're actually passing a pointer to a pointer to your buffer into the function GetDeviceState. Is that really what you want to do?
panic1227
panic1227
maybe use struct instead.
struct KeyStruct
{
char keys[256]; //maybe
};

and change your function
void di8::getKeyboardState(KeyStruct &ks)
{
pdiDevice->GetDeviceState( sizeof(ks.keys),(void*)&(ks.keys));
}
pteri498
pteri498
Well the way this would normally go, if keys[256] was in the same function as the DirectInput device, would be like this:

char keys[256];pdiDevice->GetDeviceState( sizeof(&keys),(void*)&keys);};


so the code I started with basically assumes that "buffer" would act the same way. Probably fallicious on my part, but for all the code alterations I've made, I can't figure out how to find a good set of addresses and pointers to get this to work.
etothex
etothex
Get rid of the & on front of the "buffer" variables. You're passing the pointer to a pointer to an array, not a pointer to an array.

I suggest you use a std::vector instead.
LessBread
LessBread
What happens when you try:

char keys[256];pdiDevice->GetDeviceState( sizeof(keys),(void*)keys);
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
etothex
etothex
Quote:
Original post by pteri498
Well the way this would normally go, if keys[256] was in the same function as the DirectInput device, would be like this:

*** Source Snippet Removed ***

so the code I started with basically assumes that "buffer" would act the same way. Probably fallicious on my part, but for all the code alterations I've made, I can't figure out how to find a good set of addresses and pointers to get this to work.


char keys[256];pdiDevice->GetDeviceState( 256*sizeof(char),(void*)keys);};


corrected code.
LessBread
LessBread
Quote:
Original post by etothex
Plus your sizeof(buffer) won't do exactly what you want it to(return the number of elements in your buffer). I suggest you use a std::vector instead.


In this case it will, but only because it's an array of char.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
etothex
etothex
Quote:
Original post by LessBread
Quote:
Original post by etothex
Plus your sizeof(buffer) won't do exactly what you want it to(return the number of elements in your buffer). I suggest you use a std::vector instead.


In this case it will, but only because it's an array of char.


yep, just realized that.
pteri498
pteri498
Wait no that's not the code I need fixed. That's what I would do if my life was simple. Instead, I need to pass the array into another function, much like the one I typed out in the first post in this topic. Is there a good way to fix that code?

void di8::getKeyboardState(char buffer[]){	pdiDevice->GetDeviceState( sizeof(&buffer),(void*)&buffer);};//function callinput->getKeyboardState(keys);
LessBread
LessBread
For non char types, the usual trick to determine the number of elements in an array is to divide the total number of bytes used by the array by the number of bytes in the first element of the array. For example:

sizeof(array)/sizeof(array[0])

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
LessBread
LessBread
Quote:
Original post by pteri498
Wait no that's not the code I need fixed. That's what I would do if my life was simple. Instead, I need to pass the array into another function, much like the one I typed out in the first post in this topic. Is there a good way to fix that code?
*** Source Snippet Removed ***


No. The number of elements in the array has to be determined in the scope where the array is declared. The "char buffer[]" parameter amounts to passing a const pointer, "const char *buffer", and it does so on the stack. If you perform the substitution, you'll see that sizeof(buffer) returns 4 rather than 256. Also note that sizeof operates at compile time, not runtime. Typically, the compiler supplies the sizeof value during preprocessing. Examine the preprocessor output from some sample code to confirm this for yourself.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Puzzler183
Puzzler183

void di8::getKeyboardState(char buffer[256]){	pdiDevice->GetDeviceState(sizeof(buffer), buffer);};


Have a nice day.
Enigma
Enigma
If the passed array should always contain 256 entries then pass a reference to an array of 256 entries:
void di8::getKeyboardState(char (&buffer)[256]){	// sizeof(buffer) is safer than the constant 256.  Even better would	// be to use a named constant.	pdiDevice->GetDeviceState(sizeof(buffer), reinterpret_cast< void * >(buffer));}

If the size is variable then consider a templated function:
template < std::size_t N >void di8::getKeyboardState(char (&buffer)[N]){	pdiDevice->GetDeviceState(N, reinterpret_cast< void * >(buffer));}

EDIT: I mean 'variable' as in multiple calls to the function pass references to different fixed-size arrays of different sizes, i.e. one class passes a char buffer[256] whilst another passes a char buffer[128]. If the size of one buffer can vary you obviously should be using std::vector as Nitage does below. His boost::array examples also ought to be identical in terms of generated code to my examples above. Using boost::array is probably marginally preferable to using a fixed size array.

Enigma

[Edited by - Enigma on January 24, 2006 7:49:36 AM]
Nitage
Nitage
I'd recommend either:

const size_t buffer_size = 256;void di8::getKeyboardState(std::vector<char>& buffer){    buffer.clear();    buffer.resize(buffer_size);    pdiDevice->GetDeviceState( buffer_size,(void*)&buffer[0]);};


or:

template < size_t S >void di8::getKeyboardState(boost::array<char,S>& buffer){    pdiDevice->GetDeviceState(S,(void*)&buffer[0]);};


or:

const size_t buffer_size = 256;void di8::getKeyboardState(boost::array<char,buffer_size>& buffer){    pdiDevice->GetDeviceState(buffer_size,(void*)&buffer[0]);};


probably the third option.

Topic Locked

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

Sign in to reply to this topic.