这里的struct 为什么不能省?

sevencat 2003-08-29 07:45:08
#include <stat.h>
int myfunc(const struct stat *stp)
...
(不要以为我在倒分只是我分多而已)
...全文
197 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
tonybaobao 2003-08-30
  • 打赏
  • 举报
回复
上面的强啊~~~~顶!
sevencat 2003-08-30
  • 打赏
  • 举报
回复
暂时存放地址,
要去买枝笔记下来、
http://sourceforge.net/projects/aolserver/
http://sourceforge.net/projects/srvx/
http://sourceforge.net/projects/noxwizard/
http://sourceforge.net/projects/eqemu/
http://sourceforge.net/projects/gnuworld/

sevencat 2003-08-30
  • 打赏
  • 举报
回复
结了给分了。
hongfeeling 2003-08-30
  • 打赏
  • 举报
回复
好厉害啊.
ysbcg 2003-08-30
  • 打赏
  • 举报
回复
呵呵 重名了应该是加的
chanet 2003-08-30
  • 打赏
  • 举报
回复

学习...
ejiue 2003-08-29
  • 打赏
  • 举报
回复
你想的对了.原来stat.h是unix下的头文件.而且是c风格的.
我刚才翻<<C++的设计与演进>>,无意中看到了答案.
( 为什么我自己有问题的时候没有那么幸运:) )

请看下面两段话.Bjarne Stroustrup: Page24
1."某些标准的头文件,特别是stat.h,就依赖于让struct和某个变量
或者函数取同样的名字."

2."一个名字可以指称一个类,同时可以指称一个变量或者函数.
如果某个名字真的同时指称这两种不同的东西,那么这个名字本身
指称的就是那个非类的东西,除非在前面明显的加上关键字struct,union,class."

ok.呵呵.
sevencat 2003-08-29
  • 打赏
  • 举报
回复
我一定不会设置过的,因为我自己的这个项目里面还有好多的自定义的struct呢,我也直接用了。
你们可以自己试一下好了。
<stat.h>
也是一个C库了。
我后来想会不会不写struct的话就跟stat函数重名了?

ejiue 2003-08-29
  • 打赏
  • 举报
回复
看看文件里面有没有extern c。问题可能在这里。
Jinhao 2003-08-29
  • 打赏
  • 举报
回复
对C++可以省,因为在C++中struct和class是一个东西
cxjddd 2003-08-29
  • 打赏
  • 举报
回复
在 C++ 里可以不写 struct。

在 C 里,想不写 struct 的话,就要像 FILE 那样,用 typedef 来定义。
ejiue 2003-08-29
  • 打赏
  • 举报
回复
楼主请看如下代码,在我的vc6下通过。
你的问题应该编译器使用了c的语法。
#include <iostream.h>

int g_Barrel = 0;

struct My_Info
{
int ID;
My_Info(int i){ID = i;}
};
void myfunc(const My_Info *stp)
{
g_Barrel = stp->ID;
}

void main()
{
const My_Info Test(1);
cout<<g_Barrel<<endl;
myfunc(&Test);
cout<<g_Barrel<<endl;
}
sevencat 2003-08-29
  • 打赏
  • 举报
回复
vc7.1呀,我没有改设置啊。

第一次碰到这种事感觉真是怪怪的。
一开始我直接想当然写成const stat *stp.
出错信息说*前面缺少一个,
faint
ejiue 2003-08-29
  • 打赏
  • 举报
回复
在c++下面是可以的。你的编译器用c的语法了吧。
sevencat 2003-08-29
  • 打赏
  • 举报
回复
果然stat.h里面只是
struct stat
{
dev_t st_dev; /* volume number */
ino_t st_ino; /* directory entry number of the st_name */
unsigned short st_mode; /* emulated file mode */
unsigned short st_pad1; /* reserved for alignment */
unsigned long st_nlink; /* count of hard links (always 1) */
unsigned long st_uid; /* object id of owner */
unsigned long st_gid; /* group-id (always 0) */
dev_t st_rdev; /* device type (always 0) */
off_t st_size; /* total file size--files only */
time_t st_atime; /* last access date--files only */
time_t st_mtime; /* last modify date and time */
time_t st_ctime; /* POSIX: last status change time... */
/* ...NetWare: creation date/time */
time_t st_btime; /* last archived date and time */
unsigned long st_attr; /* file attributes */
unsigned long st_archivedID; /* user/object ID of last archive */
unsigned long st_updatedID; /* user/object ID of last update */
unsigned short st_inheritedRightsMask; /* inherited rights mask */
unsigned short st_pad2; /* reserved for alignment */
unsigned int st_originatingNameSpace; /* namespace of creation */
size_t st_blksize; /* block size for allocation--files only */
size_t st_blocks; /* count of blocks allocated to file */
unsigned int st_flags; /* user-defined flags */
unsigned long st_spare[4]; /* for future use */
unsigned char st_name[255+1];/* TARGET_NAMESPACE name */
}
之类的东东。没有typedef.

另:我就是在C++下面用的。以前还没注意过这种事情呢。
gufengduyu 2003-08-29
  • 打赏
  • 举报
回复
在stat.h里是这么定义的吧
struct stat{
.....
};
sevencat 2003-08-29
  • 打赏
  • 举报
回复
stat不是直接在stat.h里面定义过了吗??
ejiue 2003-08-29
  • 打赏
  • 举报
回复
在c里面如此,在c++里面就可以省了。
请看如下的c代码:

struct S{ int a; }
int S;
void f(struct S x)
{
x.a = S;
}
摘自《C++语言的设计与演进》

这段代码在c语言里是正确的。楼主看了这段代码有什么感想?
再想想如果写成void f(S x)为什么不可以。
Jinhao 2003-08-29
  • 打赏
  • 举报
回复
就是,如果你是这样写的
typeof struct st_stat{
....
....
}STAT;
你就可以
int myfunc(const STAT *stp)
gufengduyu 2003-08-29
  • 打赏
  • 举报
回复
定义结构的时候没有用typeof定义

typeof struct st_stat{
....
....
}stat;

这样一来就可以不用struct 了

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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