_countof宏应用
来源:互联网 发布:电影票比价软件 编辑:程序博客网 时间:2024/05/18 02:43
在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]))#elseextern "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()用法
- APUE读书笔记-第10章 信号
- Remove PCSafeDoctor with WindowsUninstaller.Org Removal Tips
- Mac OS X Terminal 101:终端使用初级教程
- oracle sql优化 三 (转载)
- FF下新增event对象的srcElement、fromElement、toElement三个属性
- _countof宏应用
- JQuery在循环中绑定事件
- jquery_EasyUI的学习
- java向mysql数据库中插入中文乱码问题解决
- oracle sql优化 四(转载)
- 计算路径a对于路径b的相对路径
- js date函数详解
- mac下修改mysql的默认字符集为utf8
- 鼠标指针的隐藏