Thread: struct-property name is in char*

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    72

    struct-property name is in char*

    Hi All

    what is the best way to access the property of a struct when its name is stored in a char* ?

    It would be nice if it were something like:
    Code:
    struct TEST {
      int samples ;
    } test ;
    .....
      char *prop = "samples" ;
      test.{prop} = 10 ;
    But I guess the only/best solution is with a switch-case!

    thnx
    LuCa

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't really do that in C. You'd need something more like this:
    Code:
    struct test
    {
        char *name;
        int data;
    } t;
    
    ...do stuff...
    
    if( strcmp( t.name, "test" ) == 0 )
    {
        t.data = 10;
    }
    You can't really address structures by a string name. Meaning, you can't do:
    Code:
    printf( "What is the name of your structure? " );
    fflush( stdout );
    fgets( structurename, BUFSIZ, stdin );
    
    structurename.x = 10;
    You can't do that last line. You need to wrap it somehow (ie: store the name of 'structurename' some place in an actual structure, and then go fetch it (as showin in the first segment above)).


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no such thing in C.

    You would have to write your own code to solve that. How you would do that and what the best solution is, depends a little bit on what your structur(es) look like and what your application needs to do.

    You can't really use switch either.

    Here's a solution that may work:
    Code:
    struct somestruct
    {
        int x;
        int y;
        int z;
    };
    
    struct propentry
    {
        ptrdiff_t offset;
        const char *name;
    };
    
    #define PE(a) { offsetof(somestruct, a), #a }
    
    struct propentry proptable[] = 
    {
        PE(x),
        PE(y),
        PE(z)
    };
    
    int getProperty(char *name, somestruct *astruct)
    {
        int i;
        for(i = 0; i < sizeof(proptable)/sizeof(proptable[0]); i++)
        {
            if (strcmp(proptable[i].name, name) == 0)
            {
                 int *ptr = (int *)(((char *)astruct) + proptable[i].offset);
                 return *ptr;
            }
        }
        return -1;
    }
    I haven't tried to compile the code, so it may not work 100% straight out of the box, but it's showing the principle fairly well.

    It gets even more complicated if the content is not all integers, of course...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    72
    Mats

    could you check your code, because I cannot get it to work. I get the impression there is somethings wrong with the lines

    Code:
    #define PE(a) { offsetof(somestruct, a), #a }
    struct propentry proptable[] =
    {
        PE(x),
        PE(y),
        PE(z)
    };
    Furthermore, maybe you can explain the following line a little bit

    Code:
    int *ptr = (int *)(((char *)astruct) + proptable[i].offset);
    you cast 'astruct' to a char*, how is that possible, because its a struc!?

    I'm still learning C!

    thnx a lot
    LuCa

    ps I've added #include <stddef.h> to your code!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is missing struct in front of somestruct in a few places.

    The cast is casting a pointer to somestruct into char * - so that we can add the offset.

    I just copied and pasted the code into an editor and fixed up the errors and added a main to test it out, and it work.

    However, if you are a beginner, I'm not sure if you actually SHOULD be doing this. You are probably doing something wrong if you feel that you need to use this.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I agree, please describe the problem, not how to fix your solution.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    72
    ok, the problem is as follows. I need to use a config file. I decided to put key-value pairs in the config file, like
    Code:
    datadir=/opt/data
    samples=1024
    key=value
    ......
    I can read the file, and extract the keys and values. Next, I decided to store all this information in a structure
    Code:
    struct CONFIG {
       char * datadir ;
       int samples ;
       ....
    }
    At that point I realized that the property name was stored in a char*

    I guess with the solution provided above (I have the code working ) I should be able to store the information!

    Is this the best approach?

    LuCa

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. Property Sheets :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 05-09-2002, 03:04 PM