c++ - The sizeof several casts -
why function sizeof
not return same size when getting used on struct
itself?
i need cast because of winsock program im working on. help, true.
#include <iostream> #include <string> using namespace std; struct stringstruct { string s1; string s2; }; int main() { stringstruct ss = {"123","abc"}; char *nx = (char*)&ss; cout << sizeof(nx) << endl << sizeof(*nx) << endl; cout << sizeof(&ss) << endl << sizeof(ss) << endl; getchar(); return 0; }
the example above outputs
4 1 4 64
sizeof
tell size of given expression's type. in both sizeof(nx)
, sizeof(&ss)
, result 4 because pointers on machine take 4 bytes. sizeof(*nx)
, dereferencing char*
, gives char
, , char
takes 1 byte (and does), output 1. when sizeof(ss)
, ss
stringstruct
, size of stringstruct
, appears 64 bytes.
Comments
Post a Comment