-
why when i want copy (with memcpy)struct array to char array , character array is null??
#include <iostream> #include <string.h> using namespace std; int main() { struct group{ int num=1; int age=2;}; struct group a[17]; int m = sizeof(a); char b[200]; memcpy(&b[0],&a,sizeof(a)); cout << "Hello World" <<n<< " "<<m<<endl<<"b='"<<b[2]<<"'--"<<endl; return 0; }
you can see that output is null, why we can not copy struc array to char array??
thank in advancesh-4.2$ main Hello World 136 b=''--
[Moved to C++ Gurus ~kshegunov]
-
What are u trying to do ?
The list (group) is structs and you try to copy a struct to one char ?
That can never really work. It won't fit.The struct is 2 ints and how would that fit into one char `?
Can you explain what you want b (array) to be ?
-
@mrjj thanks
i want to copy struct to char array not one char ....
i want to use this for send on udp socket. new array char is send for other ip with this binary form,
i don't want to use Qbytearray, i should only use char array, i am strict on this method. -
@mrjj thanks
i want to copy struct to char array not one char ....
i want to use this for send on udp socket. new array char is send for other ip with this binary form,
i don't want to use Qbytearray, i should only use char array, i am strict on this method.to add to @mrjj you got a couple of issues to observe.
- byte order
- size of int
- byte alignment
In your case you are running into the byte order issue, I guess. This is dependent on processor you are using. The size problem depends on the OS and compiler. Also there is the byte alignment which changes the size of your structure. When you are using a 64 bit compiler you might problems with the size of b.
-
why when i want copy (with memcpy)struct array to char array , character array is null??
#include <iostream> #include <string.h> using namespace std; int main() { struct group{ int num=1; int age=2;}; struct group a[17]; int m = sizeof(a); char b[200]; memcpy(&b[0],&a,sizeof(a)); cout << "Hello World" <<n<< " "<<m<<endl<<"b='"<<b[2]<<"'--"<<endl; return 0; }
you can see that output is null, why we can not copy struc array to char array??
thank in advancesh-4.2$ main Hello World 136 b=''--
[Moved to C++ Gurus ~kshegunov]
@stackprogramer Instead of hacking around with low level stuff you should take a look at http://doc.qt.io/qt-5/qdatastream.html
-
why when i want copy (with memcpy)struct array to char array , character array is null??
#include <iostream> #include <string.h> using namespace std; int main() { struct group{ int num=1; int age=2;}; struct group a[17]; int m = sizeof(a); char b[200]; memcpy(&b[0],&a,sizeof(a)); cout << "Hello World" <<n<< " "<<m<<endl<<"b='"<<b[2]<<"'--"<<endl; return 0; }
you can see that output is null, why we can not copy struc array to char array??
thank in advancesh-4.2$ main Hello World 136 b=''--
[Moved to C++ Gurus ~kshegunov]
@stackprogramer
You well understand what memcpy does?memcpy(str2, str1, 10);
The above line copies the first 10 characters of str1 to str2.