[sdiy] Pointers in C

Colin f colin at colinfraser.com
Sat Dec 31 13:36:29 CET 2011


 
>   But I have a question, if I have a pointer to an unsigned 
> long int (32bits) and I want to access it as four unsigned 
> chars, do I just read a char and increase the pointer? for example;
> 
> int *my_ptr = my_unsigned_long_int;
> unsigned char FirstByte = *my_ptr;
> my_ptr++;
> unsigned char SecondByte = *my_ptr;
> my_ptr++;
> unsigned char ThirdByte = *my_ptr;
> my_ptr++;
> unsigned char FourthByte = *my_ptr;

When you do my_ptr++ it will increment by 4, since the pointer is defined as
pointing to an int.

This...

char *my_ptr;
my_ptr = (char *)my_unsigned_long_int;

unsigned char FirstByte = *my_ptr;
my_ptr++;
unsigned char SecondByte = *my_ptr;
my_ptr++;
unsigned char ThirdByte = *my_ptr;
my_ptr++;
unsigned char FourthByte = *my_ptr;

...is more what you want, I think.

Cheers,
Colin f





More information about the Synth-diy mailing list