Search code examples
cpointersbitbitwise-and

point to low byte address of variable in c


I want to learn if this is possible:

for ex:

we have long Lvalue = 0xFF00f41a;

and also have int *p;

Can we point to last 2 byte of Lvalue

like p=&Lvalue <<16;

p pointed frist 16 bit value is it possible?

*p --> f41a;
*(p+1) --->0xFF00;

then if

p = 0xa011;

long Lvalue ---> 0xFF00a011

actually I need bit operations. I have 32 bit value but I can send only 16 bits and if I change 16 bit have to change first 16 bit last 16 bit of 32 bit value.


Solution

  • Following short example will work, if int is aligned on 4-bytes (which seems to be guaranteed by gcc, see Casting an int pointer to a char ptr and vice versa):

    #include <stdio.h>                                                                                                                                                                                             
    
    int main() {                                                                                                                                                                                                   
        int v = 0xcafe;                                                                                                                                                              
        int *ip = &v;                                                                                                                                                                                              
        char *cp = (char*) ip;                                                                                                                                                                                     
    
        printf("%hhX\n", *cp); // FE on little-endian box                                                                                                                                                                                    
        printf("%hhX\n", *(cp + 1));                                                                                                                                                                               
    
        *cp = 0xbe;                                                                                                                                                                                                
        *(cp + 1) = 0xba;                                                                                                                                                                                          
        printf("%X\n", *ip);                                                                                                                                                                                       
    
        return 0;                                                                                                                                                                                                  
    }
    

    You can guarantee alignment of int thus:

    int main() {                                                                                                                                                                                                   
        __attribute__ ((aligned (4))) int v = 0xcafe;