CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: casting problem

  1. #1
    Join Date
    Sep 2003
    Posts
    815

    casting problem

    How do I cast a char* into a structure?

    I mean that I have for example the following structure

    struct myStruct
    {
    char sFirstParam[6];
    char sSecondParam[8];
    char sThirdParam[4];
    char sForthParam[2];
    };

    and I need a char* holding the string "12345612345678123412"
    I need sFirstParam to hold the first 6 chars 123456
    I need sSecondParam to hold the first 6 chars 12345678
    I need sThirdParam to hold the first 6 chars 1234
    I need sForthParam to hold the first 6 chars 12

    How do I do it without copying (memcpy for example)
    I need to do it by casting

    any idea how?

    really thanks

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    You can't. Copying is your only option with the structure you have.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Sep 2003
    Posts
    815
    when you are saying "Copying is your only option with the structure you have", you mean that with other kind of structure it is possible?
    can you give me an example?

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    The only way your going to avoid copying is to have a structure containing char*s, not char[]s. Then you simply copy the pointer values into the struct, not the contents. However, doing this will almost certainly lead to problems later, with memory leaks, pointers to invalid data, etc. Note that, if you just point to locations in the original string (e.g. sFirstParam = str; sSecondParam = str + 6; etc.), then the "substrings" in your struct will not be properly zero-terminated. If you want the substrings to be zero-terminated, then you're back to copying again. Note also that your original definition of the problem would run into trouble because your character arrays are all one character too short - i.e. they don't include room for the zero termination character.

    Why do you want to avoid the copying? If your answer includes the words "efficiency" or "optimise", then I suggest that you think again. Remeber the two rules of optimisation:

    Rule 1: don't do it.
    Rule 2: don't do it.............yet
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Aug 2002
    Posts
    58
    No, you could do a really ugly trick.
    Code:
    char *pString = "12345612345678123412";
    myStruct* pStruct = reinterpret_cast<myStruct*>(pString);
    Then use pStruct->sFirstParam, etc.

    However, remember that you still won't be able to use the str* functions on the members of the struct, because the strings won't be NULL terminated.

    And also remember that if the size of pString < sizeof(myStruct), you're probably going to crash.

    There's probably a better and safer way to do what you're trying to do.

    *edit
    And also remember that if you do exactly the above and try to write to the resulting struct, you might crash as well for trying to write into read-only memory. Compiler/OS dependent.

    Bassman

  6. #6
    Join Date
    Aug 2003
    Location
    Greece
    Posts
    206
    My little contribution on the issue ...

    The reinterpret_cast is a trick that may work under some cases but not always.

    The key is memory alignment. There is no guarantee that your structure's member variables are adjucent! Compiler may emply padding, forcing each member variable (which is also a pointer) to start on even (or multiple or 4, 8, 16) memory address.

    Code:
    struct Foo
    {
        char a[1];
        int b;
    };
    
    // The sizeof(Foo) is 8 (on most 32bit compilers)
    Regards

    --harizak

    P.S. Of course there are compiler options that alter the memory alignment policy.

  7. #7
    Join Date
    Aug 2002
    Posts
    58
    Hmmm.... when I wrote that, I disremembered myself into thinking that the structure only pads the end of the structure to stay aligned, but of course you're right - there's no guarantees.

    And like you said, various compilers will have options to change the default alignment, i.e. MSVC's #pragma pack.

    Thanks!
    Bassman

  8. #8
    Join Date
    Sep 2003
    Posts
    815
    Graham

    I don't need sFirstParam, sSecondParam, sThirdParam, sForthParam

    can you please explain to me (maybe an example) how do you cast? (suppose I have char * instead of char [])

    Thanks

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    I'm afraid I no longer understand what you're asking.

    "12345612345678123412" is a char *. if you have a char *, what is there to cast?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured