指针分配 [英] pointer assignments
问题描述
你好,
i想知道以下两个片段有什么区别
1)
{
int * p;
int * q;
p = q;
...
...
}
2)
{
int * q;
int * p = q;
...
...
}
问候,
rahul
ra ******* @ gmail.com 写道:你好,我想要的要知道以下2个片段之间有什么区别
1)
{*> int * p;
int * q;
p = q;
。 ..
...
}
2)
{
int * q;
int * p = q;
...
问候,
rahul
是的,他们真的不一样。第一个是:你分配
''q''(地址)到''p''的值已经有一个随机值。
第二个是:你声明一个int *变量p,同时你
给它分配''q''的值。下面的陈述显示
差异:
void func(int i){
静态j = 10;
j + = i;
}
与此不同:
void func(int i){
静态j;
j = 10;
j + = i;
}
#1创建两个指针并使其值保持未初始化状态。然后q
分配给p。
#2 q未初始化,p用q'的值初始化。
对所有人来说实际目的,它们是相同的。
但是,请注意,我认为两个片段都是未定义的行为
因为使用已经变量的值是非法的br />
未初始化(并将其值分配给另一个变量或
初始化另一个变量及其值将被视为
" use"。)
int * q = 0; / *或NULL如果你愿意* /
会使两个片段合法。
Alipha写道:< blockquote class =post_quotes>#1创建两个指针并使其值保持未初始化状态。然后q
分配到p。
< snip>
请引用一些上下文以便人们知道你在回复什么。
Usenet以这样一种方式工作:人们可能*永远*看不到你b $ b回复的信息,并且就自己而言,你的消息绝对没有意义。
如果你已经读了这个小组一段时间(你应该总是在发布之前做b
$ b),你会有看过很多关于如何做的指示
这个。检查CBFalconer的签名是否有一套说明。
-
Flash Gordon
生活在有趣的时代。
虽然我的电子邮件地址是垃圾邮件,但它是真的,我读了它。
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
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;
}
#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.
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.
这篇关于指针分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!