_countof宏应用
来源:互联网 发布:教育数据挖掘论文 编辑:程序博客网 时间:2024/05/21 14:00
在MSDN上面查到一个宏_countof,很好用,作用是可以直接返回静态数组的元素个数。
MSDN上这么介绍这个宏的:
_countof宏:计算静态数组元素的个数。
用法:_countof(array);
所需头文件:<stdlib.h>
示例如下:
#define _UNICODE
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
int main( void )
{
_TCHAR arr[20], *p;
printf( "sizeof(arr) = %d bytes/n", sizeof(arr) );
printf( "_countof(arr) = %d elements/n", _countof(arr) );
// In C++, the following line would generate a compile-time error:
// printf( "%d/n", _countof(p) ); // error C2784 (because p is a pointer)
_tcscpy_s( arr, _countof(arr), _T("a string") );
// unlike sizeof, _countof works here for both narrow- and wide-character strings
}
在VS2005上面输出结果如下:
sizeof(arr) = 40 bytes_countof(arr) = 20 elements
如果屏蔽掉#define _UNICODE 这句,那么输出结果又是:
sizeof(arr) = 20 bytes_countof(arr) = 20 elements
其定义如下:
/* _countof helper */
#if !defined(_countof)
#if !defined(__cplusplus)
#define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
#else
extern "C++"
{
template <typename _CountofType, size_t _SizeOfArray>
char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
#define _countof(_Array) sizeof(*__countof_helper(_Array))
}
#endif
#endif
- _countof宏应用
- _countof宏应用
- 浅析_countof宏
- 浅析_countof宏
- _countof()
- 关于_countof宏的研究
- _countof() 宏引申出来的语法知识
- _countof() 宏引申出来的语法知识
- 如何理解stdlib.h里的_countof()宏
- 如何理解stdlib.h里的_countof()宏
- 如何理解stdlib.h里的_countof()宏(转)
- 如何理解stdlib.h里的_countof()宏
- 如何理解stdlib.h里的_countof()宏
- 如何理解stdlib.h里的_countof()宏
- 如何理解stdlib.h里的_countof()宏
- 在VC6所带的stdlib.h里没有这个_countof()宏的
- _countof() 与 sizeof()
- sizeof()与_countof()用法
- ubuntu下使用国泰君安锐智版解决乱码
- 先来垫个底
- Funny fake openssh 0day
- 关于pthread_create的手册
- 如何在Vista Home家庭版中创建Administrator管理员账户
- _countof宏应用
- Ubuntu 10.04 双系统启动修复
- 彻底解决ibus和qq冲突
- 看了此篇, CSS/Style 语法规范基本够用
- 什么叫逐行扫描和隔行扫描?它们有什么优缺点?
- 异常:数据库没有有效所有者,因此无法安装数据库关系图支持对象
- 回归问题的解释
- jdbcTemplate.update(...)的用法
- OSD的主要实现方法和类型