How to printf a memory address in C

后端 未结 2 1043
名媛妹妹
名媛妹妹 2020-11-28 13:53

My code is:

#include 
#include 

void main()
    {
    char string[10];
    int A = -73;
    unsigned int B = 31337;

    strc         


        
2条回答
  •  孤街浪徒
    2020-11-28 14:51

    A workaround to use %x with length specifier to print an int or unsigned int without compiler complaining about casting would be to use malloc:

    unsigned int* D = malloc(sizeof(unsigned int)); // Allocate D
    unsigned int D_address = *((unsigned int*) &D); // D address for %08x without warning
    *D = 75; // D value
    printf("variable D is at address: %p / 0x%08x with value: %u\n", D, D_address, *D);
    

    Alternatively you can compile with gcc -w flag to suppress those casting warnings.

提交回复
热议问题