Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increment a pointer address and pointer's value?

Tags:

c

pointers

Let us assume,

int *p; int a = 100; p = &a; 

What will the following code actually do and how?

p++; ++p; ++*p; ++(*p); ++*(p); *p++; (*p)++; *(p)++; *++p; *(++p); 

I know, this is kind of messy in terms of coding, but I want to know what will actually happen when we code like this.

Note : Lets assume that the address of a=5120300, it is stored in pointer p whose address is 3560200. Now, what will be the value of p & a after the execution of each statement?

like image 455
Dinesh Avatar asked Nov 21 '11 06:11

Dinesh


People also ask

When we add 1 to the pointer its value is incremented by?

Pointer Arithmetic Unlike regular numbers, adding 1 to a pointer will increment its value (a memory address) by the size of its underlying data type. To simplify the logic behind this, think of pointer arithmetic the same way you think about array indexing.


2 Answers

First, the ++ operator takes precedence over the * operator, and the () operators take precedence over everything else.

Second, the ++number operator is the same as the number++ operator if you're not assigning them to anything. The difference is number++ returns number and then increments number, and ++number increments first and then returns it.

Third, by increasing the value of a pointer, you're incrementing it by the sizeof its contents, that is you're incrementing it as if you were iterating in an array.

So, to sum it all up:

ptr++;    // Pointer moves to the next int position (as if it was an array) ++ptr;    // Pointer moves to the next int position (as if it was an array) ++*ptr;   // The value pointed at by ptr is incremented ++(*ptr); // The value pointed at by ptr is incremented ++*(ptr); // The value pointed at by ptr is incremented *ptr++;   // Pointer moves to the next int position (as if it was an array). But returns the old content (*ptr)++; // The value pointed at by ptr is incremented *(ptr)++; // Pointer moves to the next int position (as if it was an array). But returns the old content *++ptr;   // Pointer moves to the next int position, and then get's accessed, with your code, segfault *(++ptr); // Pointer moves to the next int position, and then get's accessed, with your code, segfault 

As there are a lot of cases in here, I might have made some mistake, please correct me if I'm wrong.

EDIT:

So I was wrong, the precedence is a little more complicated than what I wrote, view it here: http://en.cppreference.com/w/cpp/language/operator_precedence

like image 123
felipemaia Avatar answered Oct 08 '22 04:10

felipemaia


checked the program and the results are as,

p++;    // use it then move to next int position ++p;    // move to next int and then use it ++*p;   // increments the value by 1 then use it  ++(*p); // increments the value by 1 then use it ++*(p); // increments the value by 1 then use it *p++;   // use the value of p then moves to next position (*p)++; // use the value of p then increment the value *(p)++; // use the value of p then moves to next position *++p;   // moves to the next int location then use that value *(++p); // moves to next location then use that value 
like image 45
Sujith R Kumar Avatar answered Oct 08 '22 02:10

Sujith R Kumar