关于指针的快速问题 [英] quick question about pointers

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

问题描述

大家好,

我有一个关于指针的快速问题。

当我说

int * p;

* p = 40;

cout<< * p;

为什么会产生错误信息(有关故障地址的信息)?

不是我创建了一个指针,我指出它的值为40和

然后显示它?!

Thanx很多,非常感谢任何帮助

玩得开心

Hello guys,
I have a quick question about pointers.
When I say
int *p;
*p = 40;
cout << *p;
why does that produce and error message(something about fault address)?
Isnt that I created a pointer and I made it point to the value of 40 and
then display it?!
Thanx alot,any help is really appreciated
Have fun

推荐答案

Snake写道:
Snake wrote:
大家好,
我有一个关于指针的快速问题。
当我说
int * p;


p现在指向什么?

* p = 40;


这就是说将40复制到位置p指向。

cout<< * p;
为什么会产生错误信息(有关故障地址的信息)?
我不是创建了一个指针而是指向40的值然后显示它? !
Hello guys,
I have a quick question about pointers.
When I say
int *p;
What does p now point to?
*p = 40;
That says "copy 40 into the location p points to".
cout << *p;
why does that produce and error message(something about fault address)?
Isnt that I created a pointer and I made it point to the value of 40 and
then display it?!




C ++的一个乐趣就是如果你不指定变量,它包含

垃圾。未来的行为是未定义的。


在这种情况下,p指向任何地方。分配可能会崩溃,或者可能会起作用 -

'未定义。


-

Phlip
< a rel =nofollowhref =http://www.xpsd.org/cgi-bin/wiki?TestFirstUserInterfacestarget =_ blank> http://www.xpsd.org/cgi-bin/wiki?Tes ... UserInterfaces


指针:


- 指向某事


- 无所事事


默认情况下,指针指向任何东西。


所以,在你的情况下,你创建一个指针没有任何意义,然后尝试

取消引用任何内容并分配值。


编译器不会将此标记为错误,实际发生的是

undefined(在受保护模式的环境中,大多数时候这会导致崩溃

类似于你所描述的)。


因此,根据您想要做的具体操作,首先必须使您的

指针指向取消引用和赋值之前的某些内容。 (

* p是取消引用,= 40分配值。)


所以,你可以将p指向已经存在的东西,例如as:


int i;

int * p =& i;

//等。


或让你的指针指向一些已分配的内存,例如:


int * p = new int;

//等。 (不要忘记通过删除释放分配的内存p

Dig?


Snake写道:
Pointers either:

- point to something

- point to nothing

By default, pointers point to nothing.

So, in your case, you create a pointer that points to nothing, then try to
dereference nothing and assign value.

The compiler will not flag this as an error, and what actually happens is
undefined (in a protected-mode environment, most times this will cause a crash
similar to what you describe).

So, depending on what specifically you want to do, you first must make your
pointer point to something prior to dereferencing and assiging a value. (The
*p is the dereference, the = 40 is assigning the value.)

So, you could point p to something that already exists, such as:

int i;
int *p = &i;
//etc.

or have your pointer point to some allocated memory, such as:

int *p = new int;
//etc. (don''t forget to release the allocated memory through delete p

Dig?

Snake wrote:

大家好,
我有一个关于指针的快速问题。
当我说
int * p;
* p = 40;
cout< ;< * p;
为什么会产生错误信息(有关故障地址的信息)?
我不是创建了一个指针,而是指向40的值,然后
然后显示它?!
Thanx很多,非常感谢任何帮助
玩得开心

Hello guys,
I have a quick question about pointers.
When I say
int *p;
*p = 40;
cout << *p;
why does that produce and error message(something about fault address)?
Isnt that I created a pointer and I made it point to the value of 40 and
then display it?!
Thanx alot,any help is really appreciated
Have fun




-

Bret Pehrson

mailto:br ** @ infowest.com

NOSPAM - 在所有电子邮件中包含此密钥<< 38952rglkwdsl>>



--
Bret Pehrson
mailto:br**@infowest.com
NOSPAM - Include this key in all e-mail correspondence <<38952rglkwdsl>>




" Snake" ha *** @ rogers.com>在留言中写道

news:50 **** **************@twister01.bloor.is.net.c able.rogers.com ...

"Snake" <ha***@rogers.com> wrote in message
news:50******************@twister01.bloor.is.net.c able.rogers.com...
你好,
我有一个关于指针的快速问题。
当我说
int * p;
* p = 40;
cout<< * p;
为什么会产生错误信息(有关故障地址的信息)?
我不是创建了一个指针而是指向40的值然后显示它? !
Hello guys,
I have a quick question about pointers.
When I say
int *p;
*p = 40;
cout << *p;
why does that produce and error message(something about fault address)?
Isnt that I created a pointer and I made it point to the value of 40 and
then display it?!



编号你创建了一个指针并指向无处(某些垃圾地址)。

然后你尝试在该位置写东西。崩溃是不可避免的。

指针在尝试与它们进行任何

操作之前应始终包含一些有效地址。


int i;

int * p;

p =& i; // OK

// p现在指向一个有效的内存地址。


或者

int * p = new INT(42); //好吧

// p现在指向一个虚拟动态商店地址。

删除p;


祝福,

Sharad


No. You created a pointer and made it point to nowhere (some garbage address).
Then you try to write something into that location. A crash is inevitable.
Pointers should always contain some valid address before you try to do any
operations with them.

int i;
int *p;
p= &i; //OK
// p is now pointing to some valid memory address.

or else
int *p = new int(42); //OK
// p now points to a vaild dynamic store address.
delete p;

Best wishes,
Sharad


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

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