tags:

views:

65

answers:

3

I simply allocate some memory for a character and wanna do then some pointer arithmetic. In this case I wanna write '\x0a' to byte 32 as follows:

#define HDR_SIZE 32   

int size = 52;

unsigned char *readXPacket = (unsigned char *) malloc (size * sizeof (unsigned char));
*readXPacket + HDR_SIZE = '\x0a';

When I try doing that I get the following error message: non-value in assignment. Anyone an idea what is wrong here?

Thanks

+6  A: 

Change your assignment to:

*(readXPacket + HDR_SIZE) = '\x0a';
Philippe Leybaert
+5  A: 

Try...

*( readXPacket + HDR_SIZE ) = '\x0a';
Paul Mitchell
+6  A: 

What's wrong with the obvious:

readXPacket[HDR_SIZE] = '\x0a';

which is both shorter and clearer. and as you are using C++, why not say:

unsigned char * readXPacket = new unsigned char[size];

Or better still:

std::vector <unsigned char> readXPacket( size );

and have C++ manage the memory for you.

anon