有个A函数,声明为
void A(stB b[]);
其中stB为一个结构类型A的调用为
...
stB c[5];
A(c);
...那么在A中,获得b的元素个数是sizeof(b) + 1 还是 sizeof(b) / sizeof(stB) ?
void A(stB b[]);
其中stB为一个结构类型A的调用为
...
stB c[5];
A(c);
...那么在A中,获得b的元素个数是sizeof(b) + 1 还是 sizeof(b) / sizeof(stB) ?
解决方案 »
- WinPcap问题 error C2733: second C linkage of overloaded function 'bpf_filter' not
- 有问题。。。。。。不帮不厚道啊。。。。感激啊。。。我新人啊 。。。
- 请大家帮我看看在对话框控件上绘图有内存泄露吗?
- sendmessage函数传递多个变量
- 高分求解:关于从包中读取图片显示的奇怪问题?
- 怎样从类向导中没有的基类中派生新类(在线等 高分)
- CreateProcess不能执行重定向任务?
- 如何将VC中PictureBox中显示的图片取出并保存成文件?
- Activex一刷新窗口就成灰色了
- 请指点 谢谢
- _RecordsetPtr在不open的情况下如何用AddNew?
- 关于文档/视图类新建问题
但是如果在函数外面调用获得数组元素个数的方法就是sizeof(b)/sizeof(stB)
但是我发现sizeof(b) 正好等于我传进去的数组的最大下标,这个怎么解释呢?
比如sizeof(int) 返回是4,因为int占4个字节.
sizeof(b)/sizeof(b[0])求的是数组元素个数
MSDN// crt_countof.cpp
#define _UNICODE
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
[code]
// crt_countof.cpp
#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
}
Copy Code
sizeof(arr) = 40 bytes
_countof(arr) = 20 elements
[/code]
// crt_countof.cpp
#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
}
sizeof(arr) = 40 bytes
_countof(arr) = 20 elements
sizeof(b)/sizeof(b[0])(你这个求的是元素个数!!!)
正好传了5个元素进去,sizeof一下等于4,其实这个4应该是一个long ptr的大小,居然那么巧