int * ptr = 10; .......问题。 [英] int* ptr=10;.......issue.

查看:73
本文介绍了int * ptr = 10; .......问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




是否有以下声明的问题,是否正确以及

究竟发生在这里。

int main()

{

// .....

int * ptr = 10; //我想ptr指的是内存位置

存储值10.

//

.......

}

在gcc上没有错误...但是当我使用g ++编译它时它会给出

" int *的无效转换to int"


请就此提出建议。

问候。

解决方案

" al pacino" < si ************* @ gmail.comwrote:


是什么问题与以下声明有关,是否正确?

正好在这里发生。

[...]

int * ptr = 10; //我想ptr指的是内存位置

存储值10.



不,这会使指针指向内存地址10

(0xa)。

gcc上的
它没有错误...但是当我用g ++编译它时它给出了

无效转换int *到int



你需要添加一个类型转换:

int * ptr =(int *)10;



int * ptr = new int [1];

* ptr = 10;

delete [] ptr;


Lars




Lars Uffmann写道:


" al pacino" < si ************* @ gmail.comwrote:


是什么问题与以下声明有关,是否正确?

正好在这里发生。

[...]

int * ptr = 10; //我想ptr指的是内存位置

存储值10.



不,这会使指针指向记忆地址10

(0xa)。



也许我很困惑,

char * s = 你好;

暗示''s''指向字符串HELLO。


但是在原始情况下......很开心是

int * ptr;和

ptr = 10; // ptr现在指向地址10;


为什么在int中就像在char中一样。


谢谢和问候。

gcc上的
它没有错误...但是当我用g ++编译它时它会给出

无效转换int *到int



您需要添加类型转换:

int * ptr =(int *)10;



int * ptr = new int [1];

* ptr = 10;

delete [] ptr;


最好的问候,


Lars








al pacino写道:


hi,


以下声明中的问题是什么,是否正确以及

究竟发生在这里。


int main()

{

// .....

int * ptr = 10; //我想ptr指的是内存位置

存储值10.



不,那是错的,而且没有 - 没有。

你不明白指针。


//

......

}


在gcc上它没有错误...但是当我用g ++编译它时它给了

无效将int *转换为int"


请就此提出建议。

问候。



这是预期的。你正在诱导未定义的行为。

你不是试图*设置*什么是*指向值为

10的指针。

你试图将* ptr中的内存地址*设置为10.

一个非常大的禁忌(谢天谢地被大多数C ++编译器捕获)。


这也是非法的:


int * ptr; // ptr保留一些剩余价值,它是酉的(坏,

禁忌,危险)

* ptr = 10;


你怎么知道剩下的位置是什么?

想象一下当编译器允许你覆盖任何内存时会发生什么

位置。

答案:UB(格式化硬盘,撞坏电脑等)

您认为这是一个常见的笑话 - 但它不是一个笑话


指针总是使用address_of(&)运算符或new来设置。


int n; //在堆栈上保留整数n,尽管n未初始化

int * ptr =& n; //在堆栈上保留指针ptr&它现在有效:它

持有n'的地址

* ptr = 10; //与n = 10相同


再次,ptr持有一个地址,* ptr是该地址的变量。

有问题的术语是 ;取消引用指针。

这种或那种方式,它设置指针的程序。

当你看到指针时,指针不是玩具,除了

多态性,一个蜂鸣器应该在你的头脑中消失。指针几乎都是坏消息。


指针是地狱

指针创建错误

指针产生头痛

a指针是一种疾病




远离指针,改为使用参考。
$ b b嗯,我们怎么解释对你的引用呢?


hi ,

whats the issue with the following declaration, is it correct and what
exactly is happening here.

int main()
{
//.....
int* ptr=10; // i suppose ptr is pointing to the memory location which
stores the value 10.
//
.......
}
on gcc it gives no error...but when i compile it using g++ it gives
"invalid conversion of int * to int"

please advise on this.
regards.

解决方案

"al pacino" <si*************@gmail.comwrote:

whats the issue with the following declaration, is it correct and what
exactly is happening here.
[...]
int* ptr=10; // i suppose ptr is pointing to the memory location which
stores the value 10.

Nope, this would make the pointer point to the memory ADDRESS 10
(0xa).

on gcc it gives no error...but when i compile it using g++ it gives
"invalid conversion of int * to int"

You need to add a typecast:
int* ptr = (int *) 10;
or
int *ptr = new int[1];
*ptr = 10;
delete [] ptr;

Best Regards,

Lars



Lars Uffmann wrote:

"al pacino" <si*************@gmail.comwrote:

whats the issue with the following declaration, is it correct and what
exactly is happening here.
[...]
int* ptr=10; // i suppose ptr is pointing to the memory location which
stores the value 10.


Nope, this would make the pointer point to the memory ADDRESS 10
(0xa).

maybe i was confused as,
char *s="hello";
implies ''s'' pointing to the string HELLO.

but in the original case..what was happning was
int *ptr; and
ptr=10; //ptr now points to address 10;

why is it so in the case of int as in char.

thanks and regards.

on gcc it gives no error...but when i compile it using g++ it gives
"invalid conversion of int * to int"


You need to add a typecast:
int* ptr = (int *) 10;
or
int *ptr = new int[1];
*ptr = 10;
delete [] ptr;

Best Regards,

Lars

or



al pacino wrote:

hi ,

whats the issue with the following declaration, is it correct and what
exactly is happening here.

int main()
{
//.....
int* ptr=10; // i suppose ptr is pointing to the memory location which
stores the value 10.

no, thats wrong and a big no-no.
You don''t understand pointers.

//
......
}
on gcc it gives no error...but when i compile it using g++ it gives
"invalid conversion of int * to int"

please advise on this.
regards.

That is expected. You are inducing undefined behaviour.
You are not trying to *set* what is *at* the pointer to the value of
10.
You are trying to set the memory address *in* ptr to 10.
A very big no-no (thankfully caught by most C++ compilers).

This too is illegal:

int *ptr; // ptr holds some residual value, its unitialized (bad,
no-no, danger)
*ptr = 10;

How do you know what is at that residual location?
Imagine what happens when the compiler lets you overwrite any memory
location.
Answer: UB (format the hard drive, crash the computer, etc)
You think thats a common joke - but its not a joke

Pointers are always set using the address_of (&) operator or new.

int n; // reserve integer n on the stack, although n is uninitialized
int *ptr = &n; // reserve pointer ptr on stack & its now valid: it
holds n''s address
*ptr = 10; // same as n = 10

Again, ptr holds an address, *ptr is the variable at that address.
And the term in question is "dereferencing the pointer".
One way or another, its the program that sets the pointer.
Pointers are not a toy, when you see a pointer, except for
polymorphism, a buzzer should go off in your head. Pointers are nearly
always bad news.

pointers are hell
pointers create bugs
pointers generate headaches
a pointer is a disease
etc

Stay away from pointers, use a reference instead.
hmm, how the hell are we going to explain references to you?


这篇关于int * ptr = 10; .......问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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