This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Using Pointer

Please could some body take a look at
my code to see why the Pointer variables
do not display correctly using debug in
Uvision2.

Here is the code:

unsigned int xdata VALUE;
unsigned int xdata *addr = &VALUE;

Main()
{
for (VALUE=1; VALUE<=10; VALUE++)
{
printf("VALUE = %d", *addr); //data
pritnf("addr1 = %d", addr); //address1
printf("addr2 = %d", &VALUE); //address2
}
}

/////////////// end //////////////

PROBLEM: VALUE at printf statement line1
works properly, but the remaining two
printf statements for printing ADDRESS1, and ADDRESS2 are always displaying 256




Parents
  • printf does not know how to print out pointers, but it knows how to print out integers. Hence, you need to convert your pointer to integer and then you can pass it to printf:

    printf("addr1 = %d", (int)addr); //address1
    printf("addr2 = %d", (int)&VALUE); //address2
    
    I don't know what 8051 xdata pointers look like, so I'm not sure if all the information contained in the pointer is preserved during conversion to int. Maybe you will have to convert them to long. Try both.
    - Mike

Reply
  • printf does not know how to print out pointers, but it knows how to print out integers. Hence, you need to convert your pointer to integer and then you can pass it to printf:

    printf("addr1 = %d", (int)addr); //address1
    printf("addr2 = %d", (int)&VALUE); //address2
    
    I don't know what 8051 xdata pointers look like, so I'm not sure if all the information contained in the pointer is preserved during conversion to int. Maybe you will have to convert them to long. Try both.
    - Mike

Children