-4

I want to understand how this part of code works i know it seems simple but I'm not good the pointer notion so anything would be helpfull

 #include<stdio.h> int main(){ int a,b; int *ptr1,*ptr2; a=5; b=a; ptr1=&a; ptr2=ptr1; b=(*ptr2)++; printf("a = %d, b=%d,*ptr1=%d,*ptr2=%d\n",a,b,*ptr1,*ptr2); } 

and the output is

a = 6 , b = 5 , *ptr1 = 6 , *ptr2 = 6. 

i need to understund thank you

5
  • 2
    The key to understanding this is b=(*ptr2)++; That takes what ptr2 is pointing to and increments it after accessing its value. Commented Sep 15, 2016 at 17:59
  • but the value of b was 5 and it didn't change ? why ? Commented Sep 15, 2016 at 18:01
  • 1
    @blitz Read up on the difference between x++ and ++x. Commented Sep 15, 2016 at 18:05
  • 1
    Because b = (*ptr2)++; is basically b = *ptr2; (*ptr2)++; or in this case b = a; a++; read about the post increment operator. Commented Sep 15, 2016 at 18:07
  • 1
    Here is the flow: 1) (*ptr2) accesses the value of what ptr2 is pointing to; 2) b is set to that value; 3) since you have post increment, after b is set to 5, then that value of what ptr2 is pointing to is incremented by 1 and is now 6. If you had ++(*ptr2) instead, then the value would be accessed, incremented, then assigned to b Commented Sep 15, 2016 at 18:09

1 Answer 1

4
#include<stdio.h> int main(){ int a,b; int *ptr1,*ptr2; a=5; // Assigns value 5 to a b=a; // Assigns value of a (i.e., 5) to b ptr1=&a; // Assigns address of a to prt1 or ptr1 points to variable a ptr2=ptr1; // ptr2 holds same address as ptr1 does (i.e, address of a) b=(*ptr2)++; /* Now this one is tricky. Look at precedence table here http://en.cppreference.com/w/cpp/language/operator_precedence b is assigned value of *ptr2 first and then value at *ptr2 (i.e., 5) is incremented later. Try replacing b = (*ptr2)++ with b = ++(*ptr2). It'll print 6. */ printf("a = %d, b=%d,*ptr1=%d,*ptr2=%d\n",a,b,*ptr1,*ptr2); } 

Let's visualize through address and value table. Suppose int is 1-byte or 1-unit and address space of your program begins with 100.

a = 5 a +---+---+---+---+---+-- | 5| | | | | ... +---+---+---+---+---+-- 100 101 102 103 104 ... b = a a b +---+---+---+---+---+-- | 5| 5| | | | ... +---+---+---+---+---+-- 100 101 102 103 104 ... ptr1=&a a b ptr1 +---+---+----+----+---+-- | 5| 5| 100| | | ... +---+---+----+----+---+-- 100 101 102 103 104 ... ptr2 holds some random address when you initialize. int *ptr2; a b ptr1 ptr2 +---+---+----+----+---+-- | 5| 5| 100| 234| | ... +---+---+----+----+---+-- 100 101 102 103 104 ... ptr2=ptr1 a b ptr1 ptr2 +---+---+----+----+---+-- | 5| 5| 100| 100| | ... +---+---+----+----+---+-- 100 101 102 103 104 ... b=(*ptr2)++ First, dereference *ptr2 and assign that to b. a b ptr1 ptr2 +---+---+----+----+---+-- | 5| 5| 100| 100| | ... +---+---+----+----+---+-- 100 101 102 103 104 ... ^ | |____________| Now increment value at address 100 a b ptr1 ptr2 +---+---+----+----+---+-- | 6| 5| 100| 100| | ... +---+---+----+----+---+-- 100 101 102 103 104 ... 

Hope that vizulization helps.

Read about pointer assignment here : C++ pointer assignment

Sign up to request clarification or add additional context in comments.

2 Comments

"// ptr2 points to ptr1" should be "Assigns ptr1 to ptr2". ptr2 does not point to ptr1. Detail: "first and then" is likely, but the order is not defined. It is more like the 2 events happen in some sequence_ (or not), but the effect is as you have commented. Order makes a difference should the variables access have a side effect.
How about this for ptr2=ptr1 : ptr2 now holds the same address as ptr1 does.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.