Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

small questions related with pointers

Posted on 2004-10-28
7
Medium Priority
?
192 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
Comment
Question by:anshuma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12440279
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
ID: 12441725
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
ID: 12441756
and what about *p++
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:anthony_w
ID: 12448901
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
ID: 12471929
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
ID: 12473045

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:
anthony_w earned 500 total points
ID: 12478507
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

618 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