Velocity Reviews > pointer assignments

# pointer assignments

rahul8143@gmail.com
Guest
Posts: n/a

 07-25-2005
hello,
i want to know what is the difference between following 2 snippets
1)
{
int *p;
int *q;
p=q;
...
...
}

2)
{
int *q;
int *p=q;
...
...
}

regards,
rahul

Cong Wang
Guest
Posts: n/a

 07-25-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> hello,
> i want to know what is the difference between following 2 snippets
> 1)
> {
> int *p;
> int *q;
> p=q;
> ...
> ...
> }
>
> 2)
> {
> int *q;
> int *p=q;
> ...
> ...
> }
>
> regards,
> rahul

Yeah,they are really different.The first one is that:you assign the
value of 'q'(an address) to 'p' which already has a random value.The
second one is that: you declare an int* variable p,at the same time you
assign the value of 'q' to it.The following statment shows the
differet:
void func(int i){
static j=10;
j+=i;
}
It is different from this:
void func(int i){
static j;
j=10;
j+=i;
}

Alipha
Guest
Posts: n/a

 07-25-2005
#1 creates two pointers and leaves their values uninitialized. then q
is assigned to p.
#2 q is left uninitialized and p is initialized with q's value.
For all practical purposes, they are identical.
Note, however, that I believe both snippets are undefined behavior
because it is illegal to use the value of a variable that has been
uninitialized (and assigning its value to another variable or
initializing another variable with its value would be considered
"use".)

int *q = 0; /* or NULL if you prefer */

would make both snippets legal.

Flash Gordon
Guest
Posts: n/a

 07-25-2005
Alipha wrote:
> #1 creates two pointers and leaves their values uninitialized. then q
> is assigned to p.

<snip>

Please quote some context so that people know what you are replying to.
Usenet works in such a way that people may *never* see the message you
are replying to and, on it's own, you message makes absolutely no sense.

Had you been reading this group for a while (which you should always do
before posting), you would have seen lots of instruction on how to do
this. Check CBFalconer's signature for one set of instructions.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.

pete
Guest
Posts: n/a

 07-25-2005
Cong Wang wrote:
>
> (E-Mail Removed) wrote:
> > hello,
> > i want to know what is the
> > difference between following 2 snippets
> > 1)
> > {
> > int *p;
> > int *q;
> > p=q;
> > ...
> > ...
> > }
> >
> > 2)
> > {
> > int *q;
> > int *p=q;
> > ...
> > ...
> > }
> >
> > regards,
> > rahul

> Yeah,they are really different.

Is that sarcasm?

> The first one is that:you assign the
> value of 'q'(an address) to 'p' which already has a random value.The
> second one is that: you declare an int* variable p,
> at the same time you
> assign the value of 'q' to it.The following statment shows the
> differet:
> void func(int i){
> static j=10;
> j+=i;
> }
> It is different from this:
> void func(int i){
> static j;
> j=10;
> j+=i;
> }

I can't understand what you're getting at.
Your functions have no return value
and the side effect is unreadable.
The static keyword changes your code example
substantially from OP's example.

--
pete

Denis Kasak
Guest
Posts: n/a

 07-25-2005
Cong Wang wrote:
>
> Yeah,they are really different.The first one is that:you assign the
> value of 'q'(an address) to 'p' which already has a random value.The
> second one is that: you declare an int* variable p,at the same time you
> assign the value of 'q' to it.The following statment shows the
> differet:
> void func(int i){
> static j=10;
> j+=i;
> }
> It is different from this:
> void func(int i){
> static j;
> j=10;
> j+=i;
> }

The example you used is really not a good analogy to the OP's question.
In your example 'j' is a static variable and static variables get
initialized only once. Because of that, the first snippet will assign to
'j' the value of 10 only once, and the second will assign it on every
entrance to 'func'. This is not equivalent to the OP's case where both
variables were non-static.

-- Denis

Cong Wang
Guest
Posts: n/a

 07-25-2005

Denis Kasak wrote:
> Cong Wang wrote:
> >
> > Yeah,they are really different.The first one is that:you assign the
> > value of 'q'(an address) to 'p' which already has a random value.The
> > second one is that: you declare an int* variable p,at the same time you
> > assign the value of 'q' to it.The following statment shows the
> > differet:
> > void func(int i){
> > static j=10;
> > j+=i;
> > }
> > It is different from this:
> > void func(int i){
> > static j;
> > j=10;
> > j+=i;
> > }

>
> The example you used is really not a good analogy to the OP's question.
> In your example 'j' is a static variable and static variables get
> initialized only once. Because of that, the first snippet will assign to
> 'j' the value of 10 only once, and the second will assign it on every
> entrance to 'func'. This is not equivalent to the OP's case where both
> variables were non-static.
>
> -- Denis

Yeah,I just show an example to prove that difference.It is bad code for
I didn't think too much of this.

Steve Summit
Guest
Posts: n/a

 07-25-2005
rahul8143 writes:
> i want to know what is the difference between following 2 snippets
> 1)
> int *p;
> int *q;
> p=q;
> 2)
> int *q;
> int *p=q;

Very, very little difference. Unless there's something tricky
you have in mind, the two snippets are for practical purposes
identical in effect.

If you're wondering about the asymmetry, why it is that (1) says
"p=q" while (2) seems to say "*p=q", you're right, that is a
little odd. Rest assured that it's p you're initializing in (2),
not *p. (Question 4.2 in the book-length version of the FAQ list

Steve Summit
(E-Mail Removed)

Denis Kasak
Guest
Posts: n/a

 07-25-2005
Cong Wang wrote:
>
> Yeah,I just show an example to prove that difference.It is bad code for
> I didn't think too much of this.

But the problem is that the examples the OP provided *were* equivalent
in the sense of the outcomes produced by them, and yours were not,
mainly because adding the 'static' keyword changes the situation
considerably.

The difference shown in your examples had nothing to do with the OP's
question.

-- Denis

Cong Wang
Guest
Posts: n/a

 07-26-2005

Denis Kasak wrote:
> Cong Wang wrote:
> >
> > Yeah,I just show an example to prove that difference.It is bad code for
> > I didn't think too much of this.

>
> But the problem is that the examples the OP provided *were* equivalent
> in the sense of the outcomes produced by them, and yours were not,
> mainly because adding the 'static' keyword changes the situation
> considerably.
>
> The difference shown in your examples had nothing to do with the OP's
> question.
>
> -- Denis

Oh? Why changes the situation? Can you say more?

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM A C++ 7 07-05-2011 07:49 PM bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM jimjim C Programming 16 03-27-2006 11:03 PM masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM