We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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
Please read the "Tips for Posting Messages" before you post a message - particularly the bit about using 'pre' and '/pre' tags when posting source code! addr and &VALUE are pointers, but the %d printf format expects an int - you can't expect to get the right results if you supply the wrong sort of argument! Go back to the manual and carefully read the sections on pointers and the printf formats.
Could you be more specific of which parameter I should use with the pointers in conjunction with printf statement. I have read manual several times but I still don't get it. Regards,
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
"printf does not know how to print out pointers" Nonsense! See p291 of the 09.2001 C51 manual: "The type field is a single character that specifies whether the argument is interpreted as a character, string, number, or pointer, as shown in the following table... "p generic * Pointer using the format t:aaaa where t is the memory type the pointer references (c: code, i: data/idata, x: xdata, p: pdata) and aaaa is the hexadecimal address"
Fair enough. I overlooked that option.
"Nonsense! See p291 of the 09.2001 C51 manual" Charming, I'm sure.
For this reason, ISO C stipulates that one should use the %p formatter for printing pointers. It's easy to remember since p is for pointer.