The Gateway to Computer Science Excellence
+1 vote
119 views
 void function ( char *a , char *b)
    {
        ++*a;
        *b++;
    }
    
    main ()
    {
        int a=5, b=5;
        function (&a, &b);
        printf ("%d,%d",a,b);
    }

Here a and b both are printing the value after execution of function code totally. Then it should print 6,6. But it is not printing that. Is preincrement and postincrement evaluate  on line of printing? Why it is not print after increment of a and b both?

Plz give brief description about this

in Programming by Veteran (119k points) | 119 views
+1
++*a; this is works as ++(*a) means do  *a= ++(5)= 6
*b++; this is works as *(b++) means increament content of b which address of variable b now if we you increment address then you pointing some garbage value i.e. not your address of actual b . then you do increamentng .

 

now value printed answer will be 6,5.

For getting 6,6  Do ++*a; (*b)++; in called function.
+1
it will work like this,

++(*a);

*(b++);

both ++ and *  are uniary operators and uniary operators are right associative..
0
Yes, its output will be 6,5. But  with *b++ I know that post incr have higher precendence so it will run first then *, and makes the value stored at b to b + sizeof(b), But it is a post increment, so why it is not executing after the expression??
0
++*a has 2 unary operators and they are right associative, so first it takes value of a=5 and increments it to 6 but for *b++ right to left associativity says execute post increment ++ first but its post-increment so it will increment b later which is actually the address of variable 'b' and just execute *b which is 5 and later increment the address. So while printing, we get 6 and 5.
0
here b is a pointer. Say, b is pointing to the address 2000. In b++ it will point to address 2001. Now, if we want to print the value it will print some garbage value. But here it is printing 5. Not garbage. What is cause of it?
0
preincrement or postincrement is not the scenario here. And that is because of dereference operator.right?
+1
See. the variable "b" and pointer "b" are different. You're incrementing the pointer "b" , a formal parameter declared in  the function. You're printing the value of variable "b" in main and by then the function call is already terminated so pointer b has no existence at all. And variable b has a value = 5, so its simply printing it.
0
@Srestha di, we are printing the value of the variable 'b' not the value at the address pointed by the pointer 'b'. Isn't it?
0
True that.

Please log in or register to answer this question.

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,324 answers
198,406 comments
105,170 users