指针初始化 [英] pointer initialization

查看:49
本文介绍了指针初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨ppl,

可以说有人说如果下面的指针初始化是合法的还是

没有。


int * intPtr = 5;


调试时我仍然看到指针被赋予了一些地址。

但是,在那里找不到初始化值。但是,在字符串初始化之后



char * charPtr ="这是一个测试字符串" ;;


很好。 charPtr指向

值的地址位置,这是一个测试字符串。


谢谢你,

Prakash

解决方案

pr *************** @ googlemail.com 写道:


can有人说如果下面的指针初始化是合法的还是

没有。


int * intPtr = 5;



不,这不合法。 `5`不是指针值(并且它没有从指针值获得的
)。编译器应该生成一个

诊断。代码的行为,如果编译器是否足够有用来生成它,则不会被C

标准指定。 (你的实现可能会说它的作用。)


在调试时我仍然看到指针被赋予了一些地址。

但是,那里找不到初始化值。



我不明白你的意思。具体。


但是,以下字符串初始化,


char * charPtr ="这是一个测试字符串" ;



这是合法的,并将charPtr分配给

的地址初始化为包含字符的静态商店

字符串(终止0)。


就好了。



好​​。


charPtr指向
$ b的地址位置$ b值,这是一个测试字符串。



是。


-

Chris" electric hedgehog" Dollin

不要向精灵求助,因为他们会回答不和是。

/指环王/


2月27日下午2:43,prakashraovadd ... @ googlemail.com写道:


你好,ppl,

可以说有人说下面的指针初始化是合法的还是

没有。


int * intPtr = 5;



你的leftside是int *,右边是int,你应该得到警告

告诉你应该施放。
< blockquote class =post_quotes>
>

调试时我仍然看到指针有一些地址。

但是,找不到初始化值那里。但是,



intPtr是内存位置,它将指向内存位置5,

如果你试图找到intPtr的地址它会有不同的价值,请参阅

这个。

#include< stdio.h>

#include< stdlib.h>


int main()

{


int * intPtr = 5; < --- Casting Warning

char * charPtr ="这是一个测试字符串" ;;


/ *这里intPtr不是EQAUL到& intPtr * /


printf(" \ n%p",& intPtr);

printf(" \ n%p \ n",intPtr);


返回0;

}

输出:


0xbffed8e4< - 某地址

0x5


------

$ b字符串初始化后$ b,


char * charPtr ="这是一个测试字符串" ;;


就好了。 charPtr指向

值的地址位置,这是一个测试字符串。


谢谢你,

Prakash



ra ************ @ yahoo.co.in 写道:


2月27日,2日:下午43点,prakashraovadd ... @ googlemail.com写道:


>嗨ppl,
有人可以说下面的指针初始化是否合法或
没有。

int * intPtr = 5;



你的leftside是int *,右边是int,你应该得到警告

告诉你应该施放。



不,你应该得到诊断[警告或不警告]说/你是什么

做错了/。它不应该告诉你施放,因为那通常是

/不是/类型不匹配的正确答案:与类型有关的事情

不匹配是/修复不匹配/。


如果OP真的想要指向一个指向

位置5的指针(无论在哪里)是 - 和/哪个/位置5那个

是什么,先生?在他的机器上如何确切地对齐?)他的实现

允许他这样做,演员可能是正确的答案。但OP没有解释为什么他们在打字游戏,所以我们

不知道这个前提是真的,我相信它是假的。


无论如何,一时兴起都是一个问题,而不是一个解决方案。是吗?


-

Chris" electric hedgehog" Dollin

不要向精灵求助,因为他们会回答不和是。

/指环王/


Hi ppl,
can some one say if the following pointer initialization is legal or
no.

int *intPtr = 5;

While debugging i still see that the pointer is given some address.
However, the initialization value is not found there. But, the
following string initialization,

char *charPtr = "this is a test string";

is just fine. The charPtr is pointing to the address location of the
value, "this is a test string".

thank you,
Prakash

解决方案

pr***************@googlemail.com wrote:

can some one say if the following pointer initialization is legal or
no.

int *intPtr = 5;

No, it''s not legal. `5` isn''t a pointer value (and it hasn''t been
obtained from a pointer value). The compiler should generate a
diagnostic. The behaviour of the code, if the compiler is
(un)helpful enough to generate it, isn''t specified by the C
standard. (Your implementation may say what it does.)

While debugging i still see that the pointer is given some address.
However, the initialization value is not found there.

I don''t understand what you mean. Be specific.

But, the following string initialization,

char *charPtr = "this is a test string";

Which is legal, and assigns to `charPtr` the address of
some static store initialised to contain the characters
of the string (with a terminating 0).

is just fine.

Good.

The charPtr is pointing to the address location of the
value, "this is a test string".

Yes.

--
Chris "electric hedgehog" Dollin
"Go not to the Elves for counsel, for they will answer both no and yes"
/The Lord of the Rings/


On Feb 27, 2:43 pm, prakashraovadd...@googlemail.com wrote:

Hi ppl,
can some one say if the following pointer initialization is legal or
no.

int *intPtr = 5;

your leftside is int* , right side is int, you should get warning
telling that you should cast.

>
While debugging i still see that the pointer is given some address.
However, the initialization value is not found there. But, the

intPtr is memory location and it will point to memory location 5,
if you try to find address of intPtr it will be different value, see
this.
#include<stdio.h>
#include<stdlib.h>

int main()
{

int *intPtr = 5; <---Casting Warning
char *charPtr = "this is a test string";

/* here intPtr is NOT EQAUL to &intPtr */

printf("\n%p",&intPtr);
printf("\n%p\n",intPtr);

return 0;
}
output :

0xbffed8e4 <--some address
0x5

------

following string initialization,

char *charPtr = "this is a test string";

is just fine. The charPtr is pointing to the address location of the
value, "this is a test string".

thank you,
Prakash



ra************@yahoo.co.in wrote:

On Feb 27, 2:43 pm, prakashraovadd...@googlemail.com wrote:

>Hi ppl,
can some one say if the following pointer initialization is legal or
no.

int *intPtr = 5;

your leftside is int* , right side is int, you should get warning
telling that you should cast.

No, you should get a diagnostic [warning or not] that says /what you''re
doing is wrong/. It shouldn''t tell you to cast, because that''s usually
/not/ the right answer to a type mismatch: the thing to do with a type
mismatch is to /fix the mismatch/.

If the OP really wants to make a pointer-to-int which points to
location 5 (wherever that is -- and /which/ location 5 would that
be, sir? Aligned how, exactly?) on his machine, and his implementation
allows him to do that, then a cast might be the right answer. But
the OP hasn''t explained why they''re playing typing games, so we
don''t know that the premise is true, and I believe it to be false.

In any case, casting at whim is a problem, not a solution. Yes?

--
Chris "electric hedgehog" Dollin
"Go not to the Elves for counsel, for they will answer both no and yes"
/The Lord of the Rings/


这篇关于指针初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆