Incrementing pointers: why doesn't address change?

The code below performs exactly as I would expect when printing array members (the chars). The pointer addresses however, are behaving not as I was expecting. The same value is printed twice. This means that after increasing the pointer, it is pointing to the right character, but the address of the pointer is still the same.

Who can tell me what's going on? Am I missing something? How can a pointer point to something else whilst keeping the same address?

void setup() {
  Serial.begin(9600);
  delay(100);

  char buff[]={'a','b','c'};
  char *pc=&buff[0];

  Serial.println((int)&pc); // prints some address
  Serial.println(*pc); // prints 'a'
  Serial.println();

  *pc++;

  Serial.println((int)&pc); // prints SAME address???
  Serial.println(*pc); // prints 'b'
}

void loop(){}

You're printing the address of the pointer. Not the address of what the pointer is pointing to.

ALSO *pc++; is actually incrementing the first character. It's not incrementing the pointer. So you're changing the first 'a' in your buffer to a 'b' Your char array will now hold "bbc";

1 Like

Don’t forget that *ptr references the data that is being pointed to, so *ptr++ increments the rvalue of what’s being pointed to. Using ptr++ increments the rvalue of the pointer itself, which increments the address being pointed to. You are actually using the pointer incorrectly. Note how I initialize the pointer to point to the base address of buff, not its contents:

Check out:

void setup() {
  Serial.begin(9600);
  delay(100);

  char buff[]={'a','b','c'};
  char *pc=&buff[0];

  Serial.println((int)&pc); // prints some address
  Serial.println(*pc); // prints 'a'
  Serial.println();

  *pc++;

  Serial.println((int)&pc); // prints SAME address???
  Serial.println(*pc); // prints 'b'
  
  //====================
  pc = buff;
  for (int i = 0; i < 3; i++) {
  Serial.print("contents: ");
  Serial.print(*pc); // prints 'a'
  pc++;
  Serial.print("   address: ");
  Serial.println((int)pc);
  }
  
}

void loop(){}

To get a better understanding of pointers, read my article here:

Thank you, both, this really helped me out. And thanks for the pointer(!) to your article, econjack.

I beg to differ Gents. *ptr++ does NOT increment the value being pointed at by ptr i.e. turn the 'a' into a 'b'.

it returns the value pointed at by ptr i.e. the 'a' which is promptly discarded and THEN the value of ptr is incremented. Now, (*ptr)++ is a different story and would do what you describe.

yaafm is right.

http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

The ++ is bound more tightly than the dereference.

 Serial.println((int)&pc); // prints SAME address???

Yes,of course it prints the same address. You are printing the address of pc - not the content of the variable pc. Try,

 Serial.println((int)pc);

in both places.

Pete

yaafm: I beg to differ Gents. *ptr++ does NOT increment the value being pointed at by ptr i.e. turn the 'a' into a 'b'.

it returns the value pointed at by ptr i.e. the 'a' which is promptly discarded and THEN the value of ptr is incremented. Now, (*ptr)++ is a different story and would do what you describe.

Well spotted.

Thirty-plus years of C programming experience and I still don't know operator precedence!