[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

small questions related with pointers

Posted on 2004-10-28
7
Medium Priority
?
194 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
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

591 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