Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6867643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:20:07+00:00 2026-05-27T03:20:07+00:00

I just want to parse some data that can be present in one of

  • 0

I just want to parse some data that can be present in one of two types. First one is

struct typeA {
  int id,
  char name[16],
  int value1,
  int value2,
  int value3,
  int value4
} __attribute__ ((packed));

second possibility is that the data has a form with double name length

struct typeB {
  int id,
  char name[32],
  int value1,
  int value2,
  int value3,
  int value4
} __attribute__ ((packed));

so far so good. Now I have two functions that parse these two

int parse_typeA(struct typeA *x){ /* do some stuff */ }
int parse_typeB(struct typeB *x){ /* do some stuff */ }

Now this is obviously impractical if you have more types. How can I realize the parsing of both types using one single function and an additional parameter like

int parse_any_type(void *x, int type){ 
    /*
     *  WHAT TO DO HERE ??
     *  
     *  The following doesn't work
     *  
     *  if(type == 1)
     *    struct typeA *a = (struct typeA *)x;
     *  else
     *    struct typeB *a = (struct typeB *)x;
     */
    printf("%i\n", a->id);
    printf("%s\n", a->name);
    printf("%i\n", a->value1);
    printf("%i\n", a->value2);
    printf("%i\n", a->value3);
    printf("%i\n", a->value4);
}

Anyone any idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:20:07+00:00Added an answer on May 27, 2026 at 3:20 am

    It depends on how general your solution must be. As other answers have identified, the two example structures are extremely similar, and therefore can be managed relatively easily (though deciding how to determine the end of the character string presents some problems).

    If you need a more general system, you’ll probably need to look at some sort of ‘structure descriptor’ string, which you pass to the converter, or possibly a ‘structure descriptor array’.

    For example, the strings might be:

    "i s16 i i i i"  // typeA
    "i s32 i i i i"  // typeB
    
    "u32 i64 z d d"  // struct { uint32_t a; int64_t b; size_t c; double d; double e; };
    
    int parse_any_type(void *output, const char *desc);
    

    You then have to deal with some alignment and padding issues, but (as long as you get the descriptor strings correct) you can write a routine to handle that lot (packed or unpacked).

    Using ‘descriptors’, you’d probably be dealing with one of the less well known macros in C, the offsetof macro defined in <stddef.h>. You’d create a descriptor type such as:

    enum Type { CHAR, UCHAR, SCHAR, STR, USTR, SSTR, SHORT, USHORT, INT, UINT, LONG, ULONG, ... };
    struct descriptor
    {
        enum Type  m_type;    // Code for the variable type
        size_t     m_size;    // Size of type
        size_t     m_offset;  // Offset of variable in structure
    };
    
    struct descriptor d_TypeA[] =
    {
        { INT, sizeof(int), offsetof(TypeA, id)     },
        { STR,          16, offsetof(TypeA, name)   },
        { INT, sizeof(int), offsetof(TypeA, value1) },
        { INT, sizeof(int), offsetof(TypeA, value2) },
        { INT, sizeof(int), offsetof(TypeA, value3) },
        { INT, sizeof(int), offsetof(TypeA, value4) },
    };
    

    You can then pass the appropriate type descriptor array (and the size of that array) to the function, along with the pointer to where the data is to be stored.

    Instead of using an enumeration, you might use a function pointer type which points to the correct converter.

    int parse_structure(void *output, const struct descriptor *desc, size_t n_desc);
    

    Another alternative is that you simply deal with each type with an appropriate function which calls other simpler functions to handle each piece of the structure.

    int parse_TypeA(TypeA *output)
    {
        if (parse_int(&output->id)      == 0 &&
            parse_str(output->name, 16) == 0 &&
            parse_int(&output->value1)  == 0 &&
            parse_int(&output->value2)  == 0 &&
            parse_int(&output->value3)  == 0 &&
            parse_int(&output->value4)  == 0)
            return 0;
        ...diagnose error...
        return -1;
    }
    

    Your examples have not clearly identified where the data comes from, as opposed to where it is to be stored. This may not matter, but will affect the solution. Given no arguments, it might be reasonable to expect the data to be read from standard input. Alternatively, you might have a string containing the data to be parsed, possibly with a length, too; these would be arguments to the function.

    Your examples have not illustrated error handling; how will the calling code know whether the conversion was successful or not.

    If done correctly, the same description mechanism can be used for both the parsing and the printing mechanisms – your parse_any_type() function looks more like a printing function.


    See Also

    • How do you use the offsetof() on a struct?
    • Need a way to alter common fields in different structs
    • What is the purpose and return type of the __builtin_offsetof() operator?
    • Why does this C code work?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write an Android application that can display some data received(polled) from
I just want my apache to register some of my predefined environment so that
I use GLib to parse some command-line options. The problem is that I want
Just want to know if there other optional wrapper framework available that generate client
I just want a simple tool that will help me quickly write scripts/packages that
I just want to clarify one thing. This is not a question on which
Today I needed to parse some data out from an xlsx file (Office open
I have a long running program, my_prog that has some output that I want
I have a MongoDB DB with some data. That all works fine, the data
I have some columns in a data store using 12 decimal precision. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.