Q:

Are the expressions *++ptr and ++*ptr same?

0

Are the expressions *++ptr and ++*ptr same?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

Both expressions are different. Let’s see a sample code to understand the difference between both expressions.

#include <stdio.h>
int main(void)
{
    int aiData[5] = {100,200,30,40,50};
    int *piData = aiData;
    ++*piData;
    printf("aiData[0] = %d, aiData[1] = %d, *piData = %d", aiData[0], aiData[1], *piData);
    return 0;
}

Output: 101 , 200 , 101

Explanation:
In the above example, two operators are involved and both have the same precedence with a right to left associativity. So the above expression ++*p is equivalent to ++ (*p). In other words, we can say it is pre-increment of value and output is 101, 200, 101.

#include <stdio.h>
int main(void)
{
    int aiData[5] = {100,200,30,40,50};
    int *piData = aiData;
    *++piData;
    printf("aiData[0] = %d, aiData[1] = %d, *piData = %d", aiData[0], aiData[1], *piData);
    return 0;
}

Output: 100, 200, 200

Explanation:
In the above example, two operators are involved and both have the same precedence with the right to left associativity. So the above expression *++p is equivalent to *(++p). In other words, you can say it is pre-increment of address and output is 100, 200,200.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program to Count Number of Words in a given stri... >>
<< Are the expressions *ptr++ and ++*ptr same?...