Pointer problem
When using pointers you can make a pointer point somwhere else by increasing it.
For example.
int array[5];
int *pointer = array;
The pointer now points to array[0].
TO make it point to array[1] you can do pointer += 1;
Right?
My problem is that I am working with void pointers and they don''t have a size so the += operation is illegal. The reason I need this is that I need to copy an unknow array to another but I have to leave one array index out. So I thought that I could copy everything up to that point then increase pointers and then copy the other bit after the index I should leave out.
DO you have a solution or a workaround for this?
quote: Original post by Mr Cucumber
My problem is that I am working with void pointers and they don''t have a size so the += operation is illegal.
...
DO you have a solution or a workaround for this?
Yes, don''t use a void pointer. I prefer to keep a byte typedef around for such things. Basically:
typedef unsigned char byte;
Since sizeof(byte) is one, you can use the += operation to skip past bytes. Just typecast to your type when you need to dereference, just like you''d have to do anyway with a void pointer.
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
If you know the size you want to step over (the type of data that''s in the array) you can just typecast the void ptr, then do the pointer arithmetic.
double array[10]
void *ptr = array;
((double*)ptr)++; // or+=1, whatever
Or if its a certain number of bytes that doesn''t correspond to a data type, just type cast as a char* and += then number of bytes
((char*)ptr) += x // x == number of bytes
double array[10]
void *ptr = array;
((double*)ptr)++; // or+=1, whatever
Or if its a certain number of bytes that doesn''t correspond to a data type, just type cast as a char* and += then number of bytes
((char*)ptr) += x // x == number of bytes
If you have an array of ANY data type, you can treat it as an array of any other data type by casting your pointer. Just remember that a 16 BYTE array can be read as 16 bytes (obviously), 4 ints or longs (unsigned or otherwise), 4 floats etc.
unsigned char * ByteArray = (unsigned char*) malloc(16);
or
unsigned char ByteArray[16];
ByteArray is a pointer to 16 bytes of memory, and can be cast like so:
ByteArray + 4 // the fifth byte and so on.
((int *) ByteArray) + 1 // the second int.
((WORD *) ByteArray) + 5 // the sixth word.
Don''t forget, if you want to write a value into that second or third int or whatever, you ''dereference'' the pointer:
*(((int *) ByteArray) + 1) = 69
unsigned char * ByteArray = (unsigned char*) malloc(16);
or
unsigned char ByteArray[16];
ByteArray is a pointer to 16 bytes of memory, and can be cast like so:
ByteArray + 4 // the fifth byte and so on.
((int *) ByteArray) + 1 // the second int.
((WORD *) ByteArray) + 5 // the sixth word.
Don''t forget, if you want to write a value into that second or third int or whatever, you ''dereference'' the pointer:
*(((int *) ByteArray) + 1) = 69
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement