sizeof() :返回所查询目标所占用字节数
_countof() :返回所查询目标所含有元素个数
_countof是Windows宏,用来计算一个静态分配的数组中的元素的个数,而sizeof是用来计算字节数,_countof(array)。
#include <iostream>
int main(int argc, char* argv[])
{
const char* a = "hello world";
char sz1[] = "hello world";
char sz2[] = { "hello world" };
char sz3[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd' };
printf_s("%d %d %d %d", sizeof(a), _countof(sz1), _countof(sz2), _countof(sz3));
std::cout << std::endl;
int b[] = { 1,2,3 };
std::cout << sizeof(b) << " " << _countof(b) << std::endl;
char str1[10] = { "hello" };
std::cout << "sizeof(str1): " << sizeof(str1) << " " << "_countof(str1): " << _countof(str1) <<" "<< "strlen(str1): " << strlen(str1) << std::endl;
system("pause");
return 0;
}
输出: