我觉得这可能与C语法有关,但我用C++开始编程,所以我不确定.
基本上我已经看到了这个:
struct tm t;
memset( &t, 0, sizeof(struct tm) );
Run Code Online (Sandbox Code Playgroud)
我对这种语法有点困惑,因为通常我希望上面的内容看起来像这样:
tm t;
memset( &t, 0, sizeof(tm) );
Run Code Online (Sandbox Code Playgroud)
这两者之间有什么区别,为什么要使用前者呢?
tm
我所指的结构是wchar.h
,其定义如下:
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
Run Code Online (Sandbox Code Playgroud)
Dav*_*eas 16
简单的答案是存在struct
关键字以限制标识符的查找tm
仅限于用户定义的类类型.可能需要与C兼容.
与其他人所说的相反,没有auto-typedef这样的东西,C和C++在管理用户定义类型的标识符方面也不一样.唯一的区别在于查找.
你可以在这里阅读更多
pmg*_*pmg 12
在C中,结构标记名称不在全局名称空间上形成标识符
struct not_a_global_identifier { /* ... */ };
Run Code Online (Sandbox Code Playgroud)
要引用该结构,您必须使用关键字struct
(以指定名称空间)
struct not_a_global_identifer object;
Run Code Online (Sandbox Code Playgroud)
或者在全局名称空间中创建一个新标识符 typedef
typedef struct not_a_global_identifer { /* ... */ } global_name_space_identifier;
Run Code Online (Sandbox Code Playgroud)
C中有4个名称空间,参见C99标准中的 6.2.3 :
这是一个合法的C程序:-)
int main(void) {
typedef struct foobar { int foobar; } foobar;
foobar boo;
boo.foobar = 42;
if (boo.foobar) goto foobar;
foobar:
return 0;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15313 次 |
最近记录: |