简体   繁体   English

C ++:指向int的指针的初始化

[英]C++: Initialization of pointer to pointer to int

Here is the sample code that I ran on Visual Studio 2010: 以下是我在Visual Studio 2010上运行的示例代码:

#include <iostream>

int main()
{
    int **p(NULL);
}

I get this error: error C2059: syntax error : 'constant' 我收到此错误: error C2059: syntax error : 'constant'

But if I change int **p(NULL); 但是如果我改变int **p(NULL); to int **p = NULL; to int **p = NULL; the above code compiles fine. 上面的代码编译得很好。

Checked this on GCC(Version:4.4.2) and both work fine. 在GCC(版本:4.4.2)上查看此内容,两者都正常。 What am I missing here? 我在这里错过了什么?

VC++ compiler seems confused about initializations of pointer to pointer ... VC ++编译器似乎对指向指针的初始化感到困惑......

This works for example 这可以作为例子

int (**p)(NULL);

These don't 这些没有

int *i;
int **p(&i);
int **o(NULL);

This works though 这虽然有效

int (**p)(&i);
typedef int* intp;
intp *o(NULL);

etc... the pattern is initialization fails whenever two ** are present! 等等...只要存在两个**,模式就会失败! I'd guess a bug! 我猜错了! Someone from MSVC team might be able to confirm 来自MSVC团队的人可能会证实

That is either a bug in the compiler itself, or possibly you've done something and asked something else. 这可能是编译器本身的一个错误,也可能是你已经做了一些事情并且问了别的东西。

MSVC10 support few features from C++11, such as the following: MSVC10支持C ++ 11中的一些功能,例如:

int **p1 = nullptr;
int **p2{}; //initialized to nullptr!

You can try any of these. 你可以尝试其中任何一个。 Both are fine. 两者都很好。

Looks like, defect with Visual studio, It works if i use c++ to compile @ http://codepad.org/ and run the following code 看起来像Visual Studio的缺陷,如果我使用c ++编译@ http://codepad.org/并运行以下代码,它就有效

int main() 
{     
    int **p(NULL); 
    return 0;
} 

Same works using g++ compiler as well. 同样的工作也使用g ++编译器。

You get a syntax error: apparently NULL is not defined. 您收到语法错误:显然没有定义NULL。 You should include cstdlib. 你应该包括cstdlib。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM