in Programming in C
633 views
0 votes
0 votes
what is the difference between

*(ptr)++ and ptr++ where char *ptr="gateoverflow"
in Programming in C
633 views

2 Answers

1 vote
1 vote
Best answer
ptr++ will make the pointer to point to 'a' in the string array. Initially,it was pointing to 'g'.

Wheras, *(ptr)++ will dereference the value at location pointed by pointer.i.e 'a'(dereference).
selected by
0 votes
0 votes
Here "precedence" is the key to understand this. Precedence of ' ++ ' is more than the deferencing operator ' * '. So putting () increases the precedence of * over ++. So, in 1st case *(p)++ it will print the next alphabet of g in ASCII sequence i.e ' h ' whereas in 2nd case *p++ will first increment the pointer then will access the value of the incremented position i.e ' a  ' for string "gateoverflow".

4 Comments

#include <stdio.h>

int main()
{
    char *ptr="gateover";
    *(ptr)++;
    printf("%c",*ptr);

    return 0;
}

o/p===  a

#include <stdio.h>

int main()
{
    char *ptr="gateover";
    *ptr++;
    printf("%c",*ptr);

    return 0;
}

op=a

in both the case op is same
0
0
It means both expression evaluates to same
0
0
Yes

() Doesn't change the priority or precedence
0
0

Related questions

0 votes
0 votes
1 answer
3