Pointers

A

arunix

Hello all ;-)
Enyone here Can tell me whts the difference between two statements :)

a.) First Statement
int val=25;
int *val1=&val;
b.) Second Statement
int val=25;
int *val1;
val =&val;

Thanks
AR.
 
N

Noah Roberts

Hello all ;-)
Enyone here Can tell me whts the difference between two statements :)

a.) First Statement
int val=25;
int *val1=&val;

create and initialize an integer with the value 25
create and initialize a pointer to an integer with the address of val.
b.) Second Statement
int val=25;
int *val1;
val =&val;

create and initialize an integer with the value 25
create a pointer to an integer.
Reassign the value of integer variable with address to itself (probably
won't compile without a cast).
 
A

arunix

its giving the error!!!!
create and initialize an integer with the value 25
create and initialize a pointer to an integer with the address of val.


create and initialize an integer with the value 25
create a pointer to an integer.
Reassign the value of integer variable with address to itself (probably
won't compile without a cast).
it works fine(compile and run fine)
 
A

arunix

Those are two statements.

[Edit: assuming you meant val1 = &val.]
ya
Those are three statements.  You've posted a total of five statements..
In (a), val1 is initialized with &val.  In (b), val1 is never
initialized, but a value is assigned to it.  This is an important
distinction in C++; for example, in the first case (a), you could as
well have declared val1 const, whereas in the latter case (b), this
would impossible.
 
A

Andrey Tarasevich

its giving the error!!!!

No, it is not "giving the error" (whatever that means). If it does not
compile in your case, your code must be different. Please, make sure
that the code you are actually working with is the same as the code you
are posting here.
it works fine(compile and run fine)

No, it does not compile. Again, please, make sure that the code you are
actually working with is the same as the code you are posting here.
 
Z

zhengtianyu1983

On Thu, 16 Apr 2009 10:15:33 -0700 (PDT)
its giving the error!!!!
Compiling these will not give any error. They are correct.
it works fine(compile and run fine)
Maybe the last statement in this piece of code is
val1 = &val;
You just posted mistakenly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top