在 C++ 中初始化指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/900262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:51:45  来源:igfitidea点击:

Initializing pointers in C++

c++pointersinitialization

提问by Alex

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

可以在声明时分配一个指向值的指针吗?像这样的东西:

    int * p = &(1000)

回答by Adam Markowitz

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

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

int *p = &(1000);

& is the address ofoperator 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;

or type-casting:

或类型转换:

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

回答by Johannes Schaub - litb

There are two things not clear in the question to me. Do you want to set the pointer to a specific value (i.e address), or do you want to make the pointer point to some specific variable?

我的问题中有两件事不清楚。您是要将指针设置为特定值(即地址),还是要使指针指向某个特定变量?

In the latter case, you can just use the address-of operator. The valueof the pointer is then set to the addressof some_int_variable.

在后一种情况下,您可以只使用 address-of 运算符。的的指针的然后被设置到地址some_int_variable

int *p = &some_int_variable;
*p = 10; // same as some_int_variable = 10;


Note: What follows is evil changing of the pointer's value manually. If you don't know whether you want to do that, you don't want to do it.

注意:接下来是手动更改指针值。如果你不知道你是否想那样做,你就不想那样做。

In the former case (i.e setting to some specific, given address), you can't just do

在前一种情况下(即设置到某个特定的给定地址),您不能只做

int *p = 1000;

Since the compiler won't take the int and interpret it as an address. You will have to tell the compiler it should do that explicitly:

由于编译器不会接受 int 并将其解释为地址。您必须告诉编译器它应该明确地这样做:

int *p = reinterpret_cast<int*>(1000);

Now, the pointer will reference some integer (hopefully) at address 1000. Note that the result is implementation defined. But nevertheless, that are the semantics and that is the way you tell the compiler about it.

现在,指针将引用地址 1000 处的某个整数(希望如此)。请注意,结果是实现定义的。但无论如何,这就是语义,这就是您告诉编译器的方式。

Update: The committee fixed the weird behavior of reinterpret_cast<T*>(0)that was suggested by a note and for which i provided a workaround before. See here.

更新:委员会修复了reinterpret_cast<T*>(0)一个笔记建议的奇怪行为,我之前为此提供了解决方法。见这里

回答by Tamara Wijsman

What about:

关于什么:

// Creates a pointer p to an integer initialized with value 1000.
int * p = new int(1000); 

Tested and works. ;-)

测试和工作。;-)

回答by Macke

Um. I don't think you can take a non-const address of a constant like that.

嗯。我认为您不能采用这样的常量的非常量地址。

but this works:

但这有效:

int x = 3;
int *px = &x;
cout << *px; // prints 3

or this:

或这个:

const int x = 3;
const int *px = &x;
const int *pfoo = reinterpret_cast<const int*>(47);