void di8::getKeyboardState(char buffer[])
{
pdiDevice->GetDeviceState( sizeof(&buffer),(void*)&buffer);
};
//function call
input->getKeyboardState(keys);
🔒 Locked
Passing array into a function
Advertisement
Original Post
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: 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?
Well the way this would normally go, if keys[256] was in the same function as the DirectInput device, would be like this:
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( 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.
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.
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.
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);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.
void di8::getKeyboardState(char buffer[256]){ pdiDevice->GetDeviceState(sizeof(buffer), buffer);};Have a nice day.
If the passed array should always contain 256 entries then pass a reference to an array of 256 entries:
If the size is variable then consider a templated function:
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]
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]
I'd recommend either:
or:
or:
probably the third option.
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.
Advertisement