473,386 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

pointer assignments

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

Nov 15 '05 #1
14 1288


ra*******@gmail.com 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;
}

Nov 15 '05 #2
#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.

Nov 15 '05 #3
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.
Nov 15 '05 #4
Cong Wang wrote:

ra*******@gmail.com 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
Nov 15 '05 #5
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

Nov 15 '05 #6


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.

Nov 15 '05 #7
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
talks about this asymmetry.)

Steve Summit
sc*@eskimo.com
Nov 15 '05 #8
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

Nov 15 '05 #9


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?

Nov 15 '05 #10
Cong Wang wrote:

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
Nov 15 '05 #11
Denis Kasak wrote:
Cong Wang wrote:

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


static variables are 3v1l!
Nov 15 '05 #12
"mike" <po*************@yahoo.com> wrote in message
news:de**********@volcano1.grnet.gr...

static variables are 3v1l!


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

--
Mabden
Nov 15 '05 #13
Mabden <mabden@sbc_global.net> wrote:
"mike" <po*************@yahoo.com> wrote in message
news:de**********@volcano1.grnet.gr...

static variables are 3v1l!


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. ;-)

--
Z (zo**********@web.de)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 15 '05 #14
On Sat, 20 Aug 2005 03:19:51 +0300, mike <po*************@yahoo.com>
wrote:
Denis Kasak wrote:
Cong Wang wrote:

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


static variables are 3v1l!


N07 n34r|y 45 3v1| 45 |337-5p34k.

Ch1|dr3n 5h0u|d 4v01d U53n37 un71| 7h3y |34rn 3n6|15h.
--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 15 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: dis | last post by:
I've been going through my code and clearing away some of the compiler warnings that i'm generating and i've come across areas where i cast pointers to integer values. The Visual Studio compiler...
10
by: Denis Palmeiro | last post by:
Hello eveyone. What exactly is a NULL Pointer Dereference? I tried searching google but the only results that returned were bug reports. Some example code, if possible, would also be...
11
by: Rajesh | last post by:
Dear All, Please let me know the advantage of function pointer? Is it fast calling function using function pointer? Is it possible to use function pointer to optimise code? Thanks and regards...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
48
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
30
by: Joe Smith | last post by:
A recent post of mine showed a sufficiently large gaffe on pointers as to need to return to K&R 5.1-6 appendix A8.6.1 . So we have type specifiers: int long .. One dreams himself variable...
3
by: Lalatendu Das | last post by:
Hi , Any way i have problem related to pointer let's say i have a double pointer like node_t **headptr =NULL ; //global one // let's say this is the defination of node_t typedef struct list {...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
11
by: Logan Lee | last post by:
It is at http://211.30.198.135/pointer_operations.html. I want to get this reviewed to make sure that they are correct. I'm not sure whether copy/paste will show up correctly but the content is: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.