pointer assignments

R

rahul8143

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
 
C

Cong Wang

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;
}
 
A

Alipha

#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.
 
F

Flash Gordon

Alipha said:
#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.
 
P

pete

Cong said:
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.
 
D

Denis Kasak

Cong said:
>
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
 
C

Cong Wang

Denis said:
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.
 
S

Steve Summit

rahul8143 said:
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
talks about this asymmetry.)

Steve Summit
(e-mail address removed)
 
D

Denis Kasak

Cong said:
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
 
C

Cong Wang

Denis said:
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?
 
D

Denis Kasak

Cong said:
Oh? Why changes the situation? Can you say more?

I already explained it in my first reply, so you should check it out one
more time.

Basically, without the static keyword, the variable would be initialized
upon each entrance of the function making it irrelevant whether you will
initialize the variable with the said value, or set it to that same
value immediately after the declaration. The static keyword changes this
behaviour.

-- Denis
 
Z

Zoran Cutura

Mabden said:
What a silly thing to say. I suppose recursion is evil as well?

And goto is evil, isn't it. ;-)

Actually coding is evil. No matter how you do it. ;-)
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top