求BYTE *byte数组个数

hu376641756 2013-01-23 11:38:39
BYTE byte[6];
byte[0]=22;
byte[1]=22;
byte[2]=22;
byte[3]=22;
byte[4]=22;
byte[5]=22;
int bLength = sizeof(byte);

为什么上面代码sizeof(byte)返回的是4,这怎么理解?如果我是想求byte数组个数,该怎么求?
...全文
370 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mujiok2003 2013-01-24
  • 打赏
  • 举报
回复

#include <string>
#include <iostream>

template <typename T, size_t SZ>
size_t Size(T (&)[SZ])
{
  return SZ;
}

struct Dummy
{
};

int main()
{ 
         Dummy d[7];
	 std::cout << Size(d) << std::endl; //7
	 return 0;
}
ForestDB 2013-01-24
  • 打赏
  • 举报
回复
数组的长度还是自己维护比较好。
赵4老师 2013-01-24
  • 打赏
  • 举报
回复
其实楼主完全可以做到一辈子都不用sizeof,照样当一个好程序员。
apexMing 2013-01-24
  • 打赏
  • 举报
回复
这样试试

对于你的sizeof(byte)是为何为4,是否你的byte也是一个数据类型呢
上一个回复搞笑了
apexMing 2013-01-24
  • 打赏
  • 举报
回复
这样试试
对于你的sizeof(byte)是为何为4,是否你的byte也是一个数据类型呢
apexMing 2013-01-24
  • 打赏
  • 举报
回复
引用
基础不够扎实啊, 朋友. 来自MSDN中的原文: The operand to sizeof can be one of the following: A type name. To use sizeof with a type name, the name must be enclosed in parentheses. An expression. When used with an expression, sizeof can be specified with or without the parentheses. The expression is not evaluated. When the sizeof operator is applied to an object of type char, it yields 1. When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier. To obtain the size of the pointer represented by the array identifier, pass it as a parameter to a function that uses sizeof. For example:
哈哈,确是如此啊。长期用糊涂了,见笑
lin5161678 2013-01-24
  • 打赏
  • 举报
回复
编译器bug 要不就是 你通过一个函数调用输出sizeof(byte)的值????
xiaohuh421 2013-01-24
  • 打赏
  • 举报
回复
引用 2 楼 luqichao 的回复:
C/C++ code?1sizeof(byte) 这里byte是数组名,也就是指针,所以sizeof(byte)是4很正常。 你说想求得数组的元素个数,对不起,这个依靠数组指针是无法求解的。 不然所谓的数组越界问题也就不会存在了。
基础不够扎实啊, 朋友. 来自MSDN中的原文: The operand to sizeof can be one of the following: A type name. To use sizeof with a type name, the name must be enclosed in parentheses. An expression. When used with an expression, sizeof can be specified with or without the parentheses. The expression is not evaluated. When the sizeof operator is applied to an object of type char, it yields 1. When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier. To obtain the size of the pointer represented by the array identifier, pass it as a parameter to a function that uses sizeof. For example:
apexMing 2013-01-24
  • 打赏
  • 举报
回复
sizeof(byte)
这里byte是数组名,也就是指针,所以sizeof(byte)是4很正常。 你说想求得数组的元素个数,对不起,这个依靠数组指针是无法求解的。 不然所谓的数组越界问题也就不会存在了。
Crazy_Number 2013-01-24
  • 打赏
  • 举报
回复
数组传入函数后会被转换为指针。 所以你在函数内sizeof(functionByte);为4,也就是指针的大小。
hu376641756 2013-01-24
  • 打赏
  • 举报
回复
这个是通过了一个函数传递后 void GetLength(BYTE *functionByte) { int bLength = sizeof(functionByte); } main() { BYTE mainbyte[6]; byte[0]="22"; .........类推 } 我是这用求的长度,照理数组变量也就是个指针,传递过去函数接收的也是同类型的指针,所以传递后functionByte的长度也应该是mainByte的长度哇,他们指向的值一样。难道这样不是按引用传递吗?
ppsharp 2013-01-23
  • 打赏
  • 举报
回复
引用 楼主 hu376641756 的回复:
BYTE byte[6];
byte[0]=22;
byte[1]=22;
byte[2]=22;
byte[3]=22;
byte[4]=22;
byte[5]=22;
int bLength = sizeof(byte);

为什么上面代码sizeof(byte)返回的是4,这怎么理解?如果我是想求byte数组个数,该怎么求?

……


我的输出是6。

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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