3

I am learning about pointers.

I was wondering how the memory address is defined in programming. I get different outputs depending on the format I choose for memory address. So I was wondering if it is correct to assume that the format of the memory address is defined by the programmer. I get different results for memory address when I use Serial.print( (long) &ptr1, DEC); and Serial.print( (long) &ptr1, HEX);

 #include <stdio.h>
 int counter = 0;
   void setup() {
    Serial.begin(9600);
   int test1 = 10; 
   float test2 = 3.14159; 
   char test3[ ] = "This is a character array";
   int *ptr1;
   float *ptr2; 
 char *ptr3; 
  ptr1 = &test1; 
Serial.print("The integer value is stored at ");
Serial.print( (long) &ptr1, DEC);

 Serial.print("The integer value is stored at ");    
  Serial.print( (long) &ptr1, HEX);


  } 
 void loop() {

    }

My output is:

The integer value is stored in 2296

The integer value is stored in 8f8

3
  • You realise you're printing the address if ptr1 here, not the test value (I think...) The formatting shouldn't make any difference - can you add example values that you get (when you print both values in the same program, re-compiling could potentially move these locations. Commented Oct 23, 2016 at 20:29
  • @Sean I have edited my question. It gives me 2296 when I put decimal format and 8f8 when I put Hex format
    – Jack
    Commented Oct 23, 2016 at 21:32
  • "Why do programmers always mix up Christmas and Halloween? Because Dec 25 is Oct 31" in your case "DEC 2296 is HEX 8f8". Commented Oct 24, 2016 at 14:33

1 Answer 1

3

You're printing the address of the pointer, not the contents of the pointer.

Say you have the memory map of:

100 20       test1 low byte
101 0        test1 high byte
102 100      ptr1 low byte
103 0        ptr1 high byte

test is located at address 100 and takes two bytes. ptr1 is located at 102 and takes two bytes. ptr1 contains the address of test (100) and test1 contains the value 20.

The different possible combinations of prints are:

Serial.println(test1);  => 20 (contents of test1)
Serial.println(&test1); => 100 (address of test1)
Serial.println(ptr1);   => 100 (contents of ptr1 = address of test1)
Serial.println(&ptr1);  => 102 (address of ptr1)
Serial.println(*ptr1);  => 20 (contents of address pointed to by ptr1).

(note: some of those will require a cast in order for the C++ to select the correct println() overload function.)

6
  • Serial.print would not work for &test, I had to put (long) before that. I get different results for Hex and Dec. I have edited my question.
    – Jack
    Commented Oct 23, 2016 at 21:33
  • I purposely omitted the casts that you would need to get the right println overloads so it didn't get too cluttered. That doesn't make any difference to what is happening though. You need to learn just what & and * mean.
    – Majenko
    Commented Oct 23, 2016 at 21:34
  • so 2296 is the same as 8f8 with different format?
    – Jack
    Commented Oct 23, 2016 at 22:08
  • You're trying to play with pointers and you don't even know the difference between decimal and hexadecimal...?!?!
    – Majenko
    Commented Oct 23, 2016 at 22:09
  • so 2296 is the same as 8f8 with different format - yes. Google 0x8f8 in decimal. You can get calculators that will do that for you. Probably is one built into your PC.
    – Nick Gammon
    Commented Oct 23, 2016 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.