Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. why when i want copy (with memcpy)struct array to char array , character array is null??

why when i want copy (with memcpy)struct array to char array , character array is null??

Scheduled Pinned Locked Moved Unsolved C++ Gurus
6 Posts 5 Posters 4.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • stackprogramerS Offline
    stackprogramerS Offline
    stackprogramer
    wrote on last edited by kshegunov
    #1

    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 advance

    sh-4.2$ main                                                                                                                                                             
    Hello World 136                                                                                                                                                          
    b=''--                                                                                                                                                                   
    

    [Moved to C++ Gurus ~kshegunov]

    jsulmJ Taz742T 2 Replies Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      1 Reply Last reply
      2
      • stackprogramerS Offline
        stackprogramerS Offline
        stackprogramer
        wrote on last edited by
        #3

        @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.

        K 1 Reply Last reply
        0
        • stackprogramerS stackprogramer

          @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.

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @stackprogramer

          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.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          4
          • stackprogramerS stackprogramer

            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 advance

            sh-4.2$ main                                                                                                                                                             
            Hello World 136                                                                                                                                                          
            b=''--                                                                                                                                                                   
            

            [Moved to C++ Gurus ~kshegunov]

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @stackprogramer Instead of hacking around with low level stuff you should take a look at http://doc.qt.io/qt-5/qdatastream.html

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3
            • stackprogramerS stackprogramer

              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 advance

              sh-4.2$ main                                                                                                                                                             
              Hello World 136                                                                                                                                                          
              b=''--                                                                                                                                                                   
              

              [Moved to C++ Gurus ~kshegunov]

              Taz742T Offline
              Taz742T Offline
              Taz742
              wrote on last edited by
              #6

              @stackprogramer
              You well understand what memcpy does?

              memcpy(str2, str1, 10);
              The above line copies the first 10 characters of str1 to str2.

              Do what you want.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved