What is the proper way to increment a pointer in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the proper way to increment a pointer in c++?

I'm a little confused about what is the proper way to increment a pointer, what is the difference between: *pointer++; *(pointer++); (*pointer)++; which one should I use? I tried the three ways in a program and the three works fine... I don't see any difference, so ... which one should I use?

7th Dec 2018, 6:03 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
2 Answers
+ 3
For me it's better to try in a program to see the differences. https://code.sololearn.com/c2KhkmjDXdrb/#c I found a summary for pointers : pointer++; // use it then move to next int position ++pointer; // move to next int and then use it ++*pointer; // increments the value by 1 then use it ++(*pointer); // increments the value by 1 then use it ++*(pointer); // increments the value by 1 then use it *pointer++; // use the value of pointer then moves to next position (*pointer)++; // use the value of pointer then increment the value *(pointer)++; // use the value of pointer then moves to next position *++pointer; // moves to the next int location then use that value *(++pointer); // moves to next location then use that value
7th Dec 2018, 6:41 AM
Prokopios Poulimenos
Prokopios Poulimenos - avatar
+ 3
If you want only increment the pointer (by one), you can do it simply: pointer++; Using also dereferentation operator (*) you get the value of pointed memory/var
7th Dec 2018, 6:39 AM
KrOW
KrOW - avatar