Pointers - incrementing and decrementing value

Hello, I am learning about pointers, and currently reading about incrementing and decrementing the value that they point to. It says to do so i can use (*pointer) ++ or (*pointer)--, and I must use the parentheses because * has a lower precedence than the ++ and --. But I am wonder what actually happens if the parentheses is not used?

I set up a small program to test it and it returns -858993460 after the increase, and 5 after the decrease, 5 is the number which it points to which i found odd. I thought that maybe the *pointer++ without the parentheses would do something to the memory address but this doesnt seem so. So does anyone know what actually happens when *pointer++ or *pointer-- is used without the parentheses.

The small program i used to test i shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  #include <iostream>

using namespace std;

int main()
{
	int *pointer;
	int Initalvar = 5;
	int NonVar;

	pointer = &Initalvar;
	cout << "Address of initalvar " << &Initalvar << "\n\n";
	cout << "Value stored at pointer " << pointer << "\n\n";
	cout << "Value pointed to by pointer " << *pointer;
	cout << "\n\n";
	cout << "Inital val value " << Initalvar << "\n\n";
	NonVar = *pointer;
	cout << "Value assigned to non var from dereferenced pointer " << NonVar << "\n\n";
	*pointer++;
	cout << "Value pointed to by pointer " << *pointer << "\n\n";
	*pointer--;
	cout << "Value pointed to by pointer " << *pointer << "\n\n";

	*pointer = 10;
	cout << "Value pointed to by pointer " << Initalvar << "\n\n";
}
pointers can point to array-like blocks of memory:
char * cp = new char[100];
char * cp2 = cp;
cp2++; //*cp2 is now cp[1], it was cp[0] before the ++.

so it increments the *pointer* (possibly into memory that is not valid) not the *value pointed to*

C coders use a lot of 'pointer math' to jump around in blocks of memory this way.
you can use it to do cool stuff, like a +- 100 range integer sort:

int * ip = new int[202];
int * bs = &(ip[100]); {0-99 = 100 items, [100] is zero, 101-201 = 100}
bs[-23]++; // this is really IP[valid], you are just going backwards from the middle...
you don't need as much of this in c++ due to containers that do most of your pointer work for you.

back to your code then:
line 19 says: increment the pointer to the next address, get its value and do nothing with it.
20 prints the value at the new address, which is junk.
21 is like 19, it goes back one (to the location you set to 5) and does nothing with the value.
then you print the valid location, still 5.
and so on.
Last edited on
Thank you that helped me understand what was going on with my code, and the values I was getting.

I have another question regarding pointers that I think touches on some of the code you showed, that I hope you could maybe help with if possible.

The book I am using showed before I must use pointer = &variable, to assign the address of the variable to the pointer. But now the book is talking about arithmetic, it is showing an array and a pointer and showing that I only have to show pointer = array, no & symbol (code shown below). But I am wondering, why I do not need to use the & symbol for this, and is it only when assigning an array to the pointer that I do not need to use the & symbol for? or are there other times that I should not use it?

1
2
3
4
int *i;
int j [10]

i = j;
the language converts the name of an array to a pointer for you as a shortcut.
so
i = j
is the same as
i = &(j[0])
it works when passing arrays to functions as well:
void foo(int *ip);
...
foo(j); //ok, j converts to int *

you cannot ressign j, though:
j = i; //fail!

I can't think of anything else right now like that, just array syntax shortcut.
be aware that char* has a funky special case:
char * s = "hello world"; //this is also a shortcut / special case. but c-style strings are best not used at all, so you probably should never use this but may see it in old code.
Last edited on
Ah ok I understand now. that is very helpful, thank you for explaining it
Topic archived. No new replies allowed.