0

after ptr++ pointer does not increment

  1 #include<iostream>
  2 
  3 int main() {
  4 
  5         char *ptr;
  6         char ch = 'A';
  7         ptr = &ch;
  8 
  9         std::cout << "pointer :" << &ptr << "\n";
 10         ptr++;
 11         std::cout << "pointer after ++ :" << &ptr << "\n";
 12         return 0;
 13 }
ikar$ g++ pointer_arth.cpp 
ikar$ ./a.out 
pointer :0x7ffeed9f19a0
pointer after ++ :0x7ffeed9f19a0
ikar$ 
R__raki__
  • 847
  • 4
  • 15
  • 30
  • You increment one thing but print another. – Jesper Juhl Nov 30 '19 at 15:04
  • @Raymond I've used pointers in C and C++ for 25+ years. I think I know how they work. Thank you. – Jesper Juhl Nov 30 '19 at 15:14
  • @JesperJuhl As the answer original had like the question, https://onlinegdb.com/SyXktWxTH As you will see using just `ptr` will not work. Now he has changed his answer to the correct response. The original answer had no code and said just change to ptr. – ABC Nov 30 '19 at 15:21
  • 1
    @Raymond, neither will `*ptr` because then you'll potentially be dereferencing memory that doesn't belong to your program – ForceBru Nov 30 '19 at 15:23
  • @ForceBru `int *a; int b = 0; a = &b;` You said just output `a` with no pointer, that would point to the memory address. But somehow you all think `*a` is wrong. Your original answer only told him to remove the reference, your first answer was wrong until it was changed. – ABC Nov 30 '19 at 23:43
  • 1
    @Raymond, yes, the first version of my answer was just as wrong as dereferencing the pointer. Indeed, your comment about testing before answering helped me understand and fix the issue with my answer. Thank you – ForceBru Dec 01 '19 at 00:02

2 Answers2

3

You're incrementing the pointer, but outputting the address of the variable that holds the pointer itself (&ptr). You should output just ptr (and format it accordingly - see edit below).

Example:

#include <iostream>

int main() {
 char data;
 char *ptr = &data;

 std::cout << "pointer:" << (unsigned long long)ptr << std::endl;
 ptr++;
 std::cout << "pointer incremented: " << (unsigned long long)ptr << std::endl;
}

Output:

pointer:140732831185615
pointer incremented: 140732831185616

Yes, printing just ptr will output garbage, so I converted the pointer to an integer (since pointers are memory addresses anyway).

As suggested in the comments, you can cast the pointer to void * when printing, which gives nicer formatting:

pointer:0x7ffee5467acf
pointer incremented: 0x7ffee5467ad0

Note how 0x7ffee5467acf == 140732745022159 != 140732831185615 - you'll get different outputs on each run because the kernel will load the executable into different places in memory.


EDIT: yes, the first version of this answer, about simply outputting ptr with std::cout << ptr, was incorrect, because the << operator is overloaded in such a way that it treats pointers to char as C-strings. Thus, that version would access potentially invalid memory and output garbage.

But the concept remains the same. Pointers to int, for example, don't have this "problem" and are printed as hexadecimal numbers, even without casting them to void *: Try it online!. The output shows that pointers are still incremented correctly by sizeof(int), which equals 4 on that machine.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
1

Pointer is incremented successfully in your code. You print the address of location which hold the pointer variable. Actually, it is garbage after character -'A' if print 'ptr', you can understand and pointing to such un-handled memory location is not good.

Build Succeeded
  • 1,153
  • 1
  • 10
  • 24