how to convert a char buffer to a struct in c++ ?

This is a discussion on how to convert a char buffer to a struct in c++ ? within the C++ Programming forums, part of the General Programming Boards category; Hi there, So i have a struct: Code: struct stinfo{ char info1[19]; char info2[5]; char info3[2]; }struct_info; and a char ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    how to convert a char buffer to a struct in c++ ?

    Hi there,

    So i have a struct:

    Code:
    struct stinfo{
     
      char info1[19];
      char info2[5];
      char info3[2];
    
    }struct_info;
    and a char buffer:

    Code:
                     
    char buf[] = "info1datainfo2i3";
    i want to cast buf to stInfo... What is the best (and safe) way to do this in C++??


    thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,786
    After you have performed this operation, what will the contents of the stinfo object be?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    i will use the first field to identify a tag and then according to the tag process the other fields. Its just a way to separate things to process.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,786
    I would then write a constructor that accepts this buf argument, inspects the tags and populates the other members.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    I see... The idea was cast and then evalue in the struct fields, using they sizes and etc... Its really not safe to do this?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,786
    Since you are going to process the other fields according to the tag, I do not see a point in trying a cast.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    i forgot to say that the stInfo will be inserted into a vector of stInfo's... So it have to be a stInfo. And when i say tags i mean ID's like "001" or "002". It will be easier to do if a just cast the buffer, but the way you said works too, process, identify, put on stInfo and the insert in the vector.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,786
    Quote Originally Posted by RLeePlusPlus
    i forgot to say that the stInfo will be inserted into a vector of stInfo's... So it have to be a stInfo.
    Well, I did not say that you should not use stInfo objects.

    Quote Originally Posted by RLeePlusPlus
    And when i say tags i mean ID's like "001" or "002".
    Yes, I understand.

    Quote Originally Posted by RLeePlusPlus
    It will be easier to do if a just cast the buffer, but the way you said works too, process, identify, put on stInfo and the insert in the vector.
    I do not see what is so difficult about:
    Code:
    char buf[] = "info1datainfo2i3";
    std::vector<stInfo> stInfoObjects;
    stInfoObjects.push_back(stInfo(buf));
    // Now stInfoObjects[0] has everything identified and processed.
    Also, have you considered using std::string objects?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    and the sizeof

    Thx for suggestions laserlight, i just have one more question...

    if i make a constructor in the struct, it will affect the result of sizeof(struct)???

    thank you!

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    2,859
    sizeof(struct) should not be affected, as long as there are no virtual methods. a simple constructor should not have any effect on the in-memory size of the struct.

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Posts
    2,598
    Quote Originally Posted by RLeePlusPlus View Post
    if i make a constructor in the struct, it will affect the result of sizeof(struct)???
    Though not strictly relevant, if you are into binary I/O and/or serialization; do not rely on anything related sizeof, for anything non POD.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert string to char or use 2d array of char?
    By simpleblue in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2011, 05:00 PM
  2. Copy pointer address to buffer and convert it back
    By vincent_rigouts in forum C Programming
    Replies: 6
    Last Post: 05-26-2011, 01:21 PM
  3. Copy struct to char buffer
    By jean.yves in forum C Programming
    Replies: 13
    Last Post: 12-15-2010, 01:00 PM
  4. Replies: 3
    Last Post: 12-06-2006, 05:59 PM
  5. Replies: 6
    Last Post: 04-14-2002, 11:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21