关于结构体的长度
我定义了一个结构题,我的想法是这样,在两个不同进程中间传递数据,由于是不同的类型,我定义一个结构体,在传递这个结构体时,需要知道这个结构体的大小,上网看了一下,都是利用sizeof()获取结构体类型的大小,那么具体大小应该怎么获取?例如:
struct a
{
CString str;
int nLevel;
}
在传递过程中,str中的值不同时,长度会不一样,有没有什么函数可以动态获取长度,还是自己在程序中计算呢?
[解决办法]
for example
#include <iostream>#include <string>using namespace std;struct A{ string str; int _a;};int getlenth(A a){ return (a.str.length()+sizeof(a._a));}int main() { A a = {"1", 3}; cout << getlenth(a)<<endl; return 0;}