Solved

small questions related with pointers

Posted on 2004-10-28
182 Views
Last Modified: 2010-04-01
               int x=48;
      int *p=&x;
      int n=(*p)++;
      cout << x << endl;


why the value of x is different ? in these two -I think it has got something to do with operator precedence - But why in the world would somebody need things like these ?

                int x=48;
      int *p=&x;
      int n=*p++;
      cout << x << endl;
0
Question by:anshuma
    7 Comments
     
    LVL 10

    Expert Comment

    by:Sys_Prog
    Yes, its because of operator precendece

    HAve a look at this link.
    It shows that the precednence of ++ is gretaer than deference [*].

    Hence the result.
    http://www.cppreference.com/operator_precedence.html


    Also, the compiler has to atke care of every possile scenaroio, hence it is designed as per certain rules, one of them being the operator precendence

    Amit
    0
     
    LVL 4

    Expert Comment

    by:anthony_w
    It is nothing to do with operator precedence.

    p is a pointer, which you initialize to point to x.

    "(*p)++" does two things. First it dereferences p, which yields a reference to x (since p is a pointer to x), then it increments the referenced item (x). Consequently, x has now been incremented.

    This is the whole purpose of pointers --- you can directly update a variable without knowing which it is. e.g.

    int x=3;
    int y=99;

    int* p=&x;
    std::cout<<*p<<std::endl; // prints 3;
    p=&y;
    std::cout<<*p<<std::endl; // prints 99

    (*p)++; // increment y
    p=&x;
    (*p)++; // increment x
    0
     

    Author Comment

    by:anshuma
    and what about *p++
    0
     
    LVL 4

    Expert Comment

    by:anthony_w
    Sorry, I missed the second code snippet in your question. I thought you were asking why the value of x changed.

    *p++ is different to (*p)++ due to operator precedence as suggested.

    *p++ is equivalent to *(p++), which returns the result of *p, then increments p. Consequently x is unchanged, but p is incremented in the second example, whereas x is incremented but p is unchanged in the first example.

    Note that in both cases, n is the same, since post increment (something++) returns the value before the increment.
    0
     
    LVL 2

    Expert Comment

    by:arun80_inin
    I will explain with one example

    assume the addres  of x is 1000.

    the value of x is 48.

    int *p=&x    in this stmt you are assigning the address of x.

    so for the statent   (*p)++  the value in the address of x will be increment by 1

    but in the case of statement  *p++    the address of x will be added by 2 and it returns the value stored in the location 1002.

    Note :
    Since it is integer if you increment the adress by 1 it will automatically incremented by 2 because integer needs 2 bytes.

    pls post ur comment for this answer
    0
     

    Author Comment

    by:anshuma

    int x=48;
         int *p=&x;

           cout << &x << endl;
         cout << x << endl;

           int n=*p++;

           cout << &x << endl;
         cout << x << endl;


    If the address of x was increased by 2 then x should have some garbage value in it. Nothing happens to the address of x by *p++ at least in visual C++ 6.0
    0
     
    LVL 4

    Accepted Solution

    by:
    The expression *p++ does not affect the value of x. It dereferences and increments p; it is equivalent to the two expressions *p and p++.

    int n=*p++; //1

    int n=*p; //2
    p++;

    //1 and //2 are both equivalent.

    This is really only useful in the case of arrays,in which case it moves p to point to the next entry:

    int someArray[10];

    int *p=&someArray[0]; //get address of first element

    someArray[0]=3;

    int n=*p++;
    cout<<n<<endl; //prints 3
    cout<<p<<endl; // this prints the same as
    cout<<&someArray[1]<<endl; // this
    someArray[1]=5;
    cout<<*p<<endl; // prints 5
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    845 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    3 Experts available now in Live!

    Get 1:1 Help Now