Sign in to follow this  

a simple stupid question :)

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay i got this simple (and stupid) question i got a char * data contains several bytes.. lets say 8 (size changes though), would be 01 02 03 04 05 06 07 08 okay, i need to run a switch on it, like switch(data) { case 0x01: blabla; return; }; now, i need to get the first byte only, (0x01) to check it.. like data[0], but that wont work (or atleast aint working here). anyone got an idea of super quickly getting the first byte only into the swich check ? Thanks

Share this post


Link to post
Share on other sites
Subscripting should definately work, provided your pointer is valid...

#include <iostream>
using namespace std;

int main()
{
char buff [] = { 0x0, 4, 3, 1, 6, };
char* ptr = buff;
switch(ptr[0])
{
case 0x0:
cout << "Zero" << endl;
break;
default:
cout << "Nonzero" << endl;
break;
}
return 0;
}


Share this post


Link to post
Share on other sites
Chris Hare is right. When you did
switch (data)
you were actually switching based on the value of "data", which is simply a pointer. What you want to switch on is what data points to, which is
data[0]
or equivalently
*data

Share this post


Link to post
Share on other sites
still aint working, here is what i got so far


void checkout(char* buffer)
{
char * ptr = buffer;

switch(ptr[0])
{
// general stuff
case 0x67:
do something
break;
default:
do something else
break;
};
}



now, if my buffer looks like this

67 00 00 01 20 22 02 00
it will use default

if my buffer looks like this
67
it will use the case 0x67

Share this post


Link to post
Share on other sites

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this