关于int()和int型指针

forwchen 2012-12-10 04:28:40

#include <iostream>
using namespace std;

int f(int*)
{

}

int main(){

int *a=0;
a=int();

return 0;
}






以上程序编译通过了,想问一下int()为什么是int型的指针
还有函数的参数为什么可以是这个样子 int f(int*) ?
...全文
226 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yisikaipu 2012-12-10
  • 打赏
  • 举报
回复
啧,找给你最权威的解释辛苦分都没有,情何以堪……
dhdahai 2012-12-10
  • 打赏
  • 举报
回复
引用 5 楼 yisikaipu 的回复:
引用 楼主 forwchen 的回复:想问一下int()为什么是int型的指针 int()的类型是整型,但标准允许整型可显式转换为指针 $5.2.10/5 A value of integral type or enumeration type can be explicitly converted to a pointer. ...
++
yisikaipu 2012-12-10
  • 打赏
  • 举报
回复
另外,这里有个未定义行为 int f(int*) { } 标准认为,应返回却无return的属未定义行为 $6.6.3/2 ... Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.
yisikaipu 2012-12-10
  • 打赏
  • 举报
回复
引用 楼主 forwchen 的回复:
想问一下int()为什么是int型的指针
int()的类型是整型,但标准允许整型可显式转换为指针 $5.2.10/5 A value of integral type or enumeration type can be explicitly converted to a pointer. ...
woshiwaiwai 2012-12-10
  • 打赏
  • 举报
回复
楼上正解。。。
popy007 2012-12-10
  • 打赏
  • 举报
回复
int()不是个指针,就是一个int值,你可以把它看作int的"default ctor",它是0。你相当于把一个int值0赋予了指针a int* a = 0; 这没什么问题。 int f( int * ) {} 定义的时候省略了形参名称,这也没什么问题,C++允许这样。代价就是你无法在这个scope里面使用这个参数了。
rocktyt 2012-12-10
  • 打赏
  • 举报
回复
int()是创建一个0初始化的int型临时变量,也就是0,指针可以赋值为0 函数形参如果函数内没有用到可以省略变量名,还用声明时也可以省略
yisikaipu 2012-12-10
  • 打赏
  • 举报
回复
引用 楼主 forwchen 的回复:
还有函数的参数为什么可以是这个样子 int f(int*) ?
C++标准允许形参名字可不写 $8.3.5/8 An identifier can optionally be provided as a parameter name; if present in a function definition (8.4), it names a parameter (sometimes called “formal argument”). [Note: in particular, parameter names are also optional in function definitions and names used for a parameter in different declarations and the definition of a function need not be the same. ...

64,333

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧