How to convert a structure name into a string and vice versa?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Carrow
    New Member
    • May 2013
    • 4

    How to convert a structure name into a string and vice versa?

    Hi,

    I need to convert a tsructure name into a string and vice versa. I don't really know how to do that in c.
    Could anyone help me for that?

    Thanks in advance,
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Hi Carrow,

    Structures, by their nature are binary objects, not strings.

    Do you just want the raw bytes of the structure, or are you trying to do something else?

    At a first blush, you can use something like this:
    Code:
    tstructure obj;
    void * pointer0 = (void *)&obj;
    char * pointer1 = (char *)pointer0;
    Hope that helps a little,
    Oralloy

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      First you need to convert each struct member to a string.

      Next you can use strcpy and strcat to build your final string.

      Now in order to decode the final string later, insert a marker between each of the members. A comma is frequently used:

      data, data, data, etc...

      A string like this is called a CSV string for comma-separated-values.

      You will need this marker to break up the final string into the members that it was made from.

      Code:
      strcpy(dest, str1);
      strcat(dest, ",");
      strcat(dest, str2);
      strcat(dest, ",");
      etc...
      Here dest is the final string. dest only needs to be large enough to hold str1:

      Code:
      dest = malloc(sizeof(strlen(str1) + 1));
      Note that strlen only returns the number of characters in str1 so you need to add 1 to make room for the null terminator that will be placed into dest by that first strcpy.

      After that, strcat will take care of adding memory as needed to dest as dest grows in size as the member strings are appended.

      Comment

      • Carrow
        New Member
        • May 2013
        • 4

        #4
        Hi everyone,
        Thanks a lot for all your answers.
        Actually, I'm working on a function that receive as parameter a structure. Then, I need to compare the 4 first letters of the struct parsed name, that why I need to convert a struct name into string.

        The structures I'm using, contain a lot of members (unsigned long, pointers...), Do you think that the only way is to convert each member?

        Carrow

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Whether you have to convert all members depends on what the comparison is supposed to tell you. What do those first 4 letters mean? If you want to check, whether two struct instances are the same, you will need to create some kind of hash function (which is a function which creates an identifier which is hopefully unique for each instance). If you only want to compare whether it contains something specific you can probably do much less.

          Comment

          • Carrow
            New Member
            • May 2013
            • 4

            #6
            The four first letters indicate which configuration should I point into, what come after indicate other specifications for my function.
            The problem with my code is that I'm not free to use whatever I want, and juste to make it works...

            I found a way to avoid the use of structure name conversion into a string...But, I'm still interesting to know how to do that...

            Thanks again for your answers :-)

            Comment

            • Oralloy
              Recognized Expert Contributor
              • Jun 2010
              • 988

              #7
              Carrow,

              It sounds like you have a function that takes an arbitrary structure, perhaps as a void pointer, and then dispatches activity based on the type of the structure?

              Is that the problem that you are trying to solve?

              Oralloy

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                You can use strncpy to compare just the first 4 letters.

                Code:
                struct x
                {
                   char name[80];
                };
                
                x obj;
                etc...
                
                strncpy(obj.name, "Mike", 4);

                Comment

                • Carrow
                  New Member
                  • May 2013
                  • 4

                  #9
                  Oralloy,
                  My function receive as parameter the name of the structure (with specific instantiation). I just wanted to compare the structure name to do specific tasks.
                  That why I needed to convert the structure name into a string, to make this comparison.

                  But actually, I found another way to do that. Instead of comparing and then doing specific tasks and configurations, I used, another structure that includes configurations I needed and I used a pointer in my structure that pointed into this one.

                  :-)

                  Thanks again,

                  Carrow

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    There is no structure name at run time. All the code has been converted to binary machine instructions.

                    Why is the structure name important at run time?

                    Comment

                    • tanvalley
                      New Member
                      • Jun 2012
                      • 17

                      #11
                      If you were to switch to a c++ compiler (and you can do this without changing to an obect-oriented design) then using the GNU compilers, you can include
                      Code:
                      const char* classname(void) {return __PRETTY_FUNCTION__; }
                      in every structure of interest. Then call it (parameter.clas sname()) and parse out the stucture name (just before the ::).

                      Your mileage may vary, depending on the compiler you're using, and it's not just a matter of changing to a c++ compiler, there being language differeces that can bite the unwary.

                      And it's bound to be more complicated than just this. And you can't get the name of the variable, just the structure type.

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        In a C++ program you should never need to know the type of something. If you do need to know, it indicates there may be problems with your C++ design. This is especially true of the names of structure types.

                        However, in the debugging phase of development, you may need to know this information so it is available at all times through the debugger.

                        Once you go to release, if there is code to provide a structure name, then you have frozen your application. On objective of C++ is to allow old programs to use new code without needing to be recompiled.

                        Comment

                        Working...