在C ++中初始化指针 [英] Initializing pointers in C++

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

问题描述

可以在声明时将指针分配给值吗?像这样:

Can assign a pointer to a value on declaration? Something like this:

    int * p = &(1000)

推荐答案

是的,您可以在声明时初始化指向值的指针,但是不能这样做:

Yes, you can initialize pointers to a value on declaration, however you can't do:

int *p = &(1000);

&是运算符的地址,您不能将其应用于常量(尽管可以的话,这很有趣).尝试使用另一个变量:

& is the address of operator and you can't apply that to a constant (although if you could, that would be interesting). Try using another variable:

int foo = 1000;
int *p = &foo;

或类型转换:

int *p = (int *)(1000); // or reinterpret_cast<>/static_cast<>/etc

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

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