Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers problem

Tags:

c

pointers

What is the difference b/w

struct {
    float *p; 
}*ptr=s;

*ptr->p++

and

(*ptr->p)++;

I understand that the former points to the next address while the latter increments the value by 1 but I cannot get how it is happening.....

like image 334
manugupt1 Avatar asked Mar 27 '26 08:03

manugupt1


2 Answers

It's all about precedence.

In the first example you are incrementing the location that *p points to.

In the second example you are dereferencing *p and incrementing the value.

like image 143
Quintin Robinson Avatar answered Mar 29 '26 20:03

Quintin Robinson


Due to C operator precedence,

*ptr->p++;

is equivalent to

*(ptr->p++);

so it actually increments the pointer, but dereferences the original address, due to the way postfix ++ works.

However, since nothing is done to the dereferenced address, the statement is equivalent to

ptr->p++;
like image 31
Artelius Avatar answered Mar 29 '26 20:03

Artelius