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.
  • S Offline
    S Offline
    stackprogramer
    wrote on 11 Jun 2017, 14:17 last edited by kshegunov 6 Nov 2017, 14:26
    #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]

    J T 2 Replies Last reply 12 Jun 2017, 06:15
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 11 Jun 2017, 14:45 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
      • S Offline
        S Offline
        stackprogramer
        wrote on 11 Jun 2017, 16:20 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 11 Jun 2017, 16:55
        0
        • S stackprogramer
          11 Jun 2017, 16:20

          @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 11 Jun 2017, 16:55 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
          • S stackprogramer
            11 Jun 2017, 14:17

            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]

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 12 Jun 2017, 06:15 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
            • S stackprogramer
              11 Jun 2017, 14:17

              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]

              T Offline
              T Offline
              Taz742
              wrote on 1 Jul 2017, 13:04 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