C Programming - Pointers - Discussion

Discussion Forum : Pointers - General Questions (Q.No. 8)
8.
Can you write the another expression which does the same job as ++*ptr?
(*ptr)++
++*(ptr)
(ptr)*++
(ptr)++*
Answer: Option
Explanation:
#include<stdio.h>

int main()
{
    int a = 10, b = 10;
    int c,d;
    int *ptra = &a;
    int *ptrb = &b;

    ++*ptra;
    (*ptrb)++;

    printf("\n A = %d , B = %d", a , b);

    c = ++*ptra;
    d = (*ptrb)++;

    printf("\n A = %d , B = %d", a , b);
    printf("\n C = %d , D = %d", c , d);

    return 0;
}

/* Output:

 A = 11 , B = 11
 A = 12 , B = 12
 C = 12 , D = 11
*/
Discussion:
6 comments Page 1 of 1.

Sundar said:   2 decades ago
Hi Prachi Deshmukh,

c = ++*ptra; --> This statement increments the value of 'a' and assings it to 'c'. Therefore 'a' and 'c' are equal.


d = (*ptrb)++; --> This statement assigns the value of 'b' to 'd',
then increases the value of 'b'. So, here 'd' is less than 'b'.

Pragatheeswaran said:   2 decades ago
If we use with directly in printf() ++*ptr is should equal to ++*(ptr).

Ex:

printf("%d", ++*ptr) is equal to printf("%d", ++*(ptr))

but not equal to printf("%d",(*ptr)++)

Muhammad Alshad said:   2 decades ago
thanks sundar and pragtheesh

Prachi Deshmukh said:   2 decades ago
How the value of D is 11?

Chandana said:   2 decades ago
thanks sundar

Padma said:   2 decades ago
thanks sundar

Post your comments here:

Your comments will be displayed after verification.