Thread: Saving data in a struct field depending on the data introduced

  1. #1
    Registered User
    Join Date
    Nov 2022
    Posts
    5

    Saving data in a struct field depending on the data introduced

    Hi!

    I'm trying to make a program in which the user introduces a string and then some data is stored in field of the same name.

    I'll explain myself writing some code.
    Code:
    struct Data{
      int ab;
      int ba;
      int ca;
    }
    
    int main{
    
      char input[20];
      int a=1;
      struct Data example;
    
      printf("Introduce the field: ");
      scanf("%s",input);
    
      if(strcmp(input,"ab")==0)
        example.ab=a;
    
      /*I'm doing so for every field*/
    
      return 0;
    }
    The example I'm offering it's easier than the program I have to do. Bear in mind that in my program I'm using more than 20 strcmp.

    Is there a more efficient way of doing so?

    Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,433
    There are a few ways you could go.

    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <stddef.h>
    #include <string.h>
    
    // All the values are the same type
    struct Pair {
        const char *name;
        int value;
    };
    
    struct Pair example[3] = {
        { "ab", 0 },
        { "ba", 0 },
        { "ca", 0 },
    };
    
    void assign(struct Pair data[], size_t len, const char *name, int value) {
        for ( size_t i = 0 ; i < len ; i++ ) {
            if ( strcmp(data[i].name, name) == 0 ) {
                data[i].value = value;
            }
        }
    }
    
    // All the values have different types
    struct Pair2 {
        const char *name;
        int         offset;
        enum {
            T_INT,
            T_DOUBLE,
            T_CHAR,
        }           type;
    };
    
    // Your data, with heterogenous types
    struct Data {
        int     ab;
        double  ba;
        char    ca;
    };
    
    struct Pair2 map[3] = {
        { "ab", offsetof(struct Data,ab), T_INT },
        { "ba", offsetof(struct Data,ba), T_DOUBLE },
        { "ca", offsetof(struct Data,ca), T_CHAR },
    };
    
    void assign2(struct Pair2 map[], size_t len, const char *name, struct Data *instance, int value) {
        for ( size_t i = 0 ; i < len ; i++ ) {
            if ( strcmp(map[i].name, name) == 0 ) {
                switch ( map[i].type ) {
                    case T_INT:
                    {
                        int *p = (int*)((char*)instance + map[i].offset);
                        *p = value;
                        break;
                    }
                    case T_DOUBLE:
                    {
                        double *p = (double*)((char*)instance + map[i].offset);
                        *p = value;
                        break;
                    }
                    case T_CHAR:
                    {
                        char *p = (char*)((char*)instance + map[i].offset);
                        *p = value;
                        break;
                    }
                }
            }
        }
    }
    
    int main ( ) {
        assign( example, 3, "ba", 42);
        struct Data instance;
        assign2( map, 3, "ba", &instance, 42);
        printf("%d %f\n", example[1].value, instance.ba);
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem by loading and saving data to struct :/
    By ibMandForhelved in forum C Programming
    Replies: 6
    Last Post: 02-18-2016, 11:38 PM
  2. Get data from a .txt file field by field...
    By IndioDoido in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 06:07 PM
  3. Saving Data
    By JoeJTaylor in forum C++ Programming
    Replies: 5
    Last Post: 04-22-2006, 09:06 PM
  4. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  5. Saving my data
    By redsoxj in forum C Programming
    Replies: 7
    Last Post: 05-09-2002, 09:06 PM

Tags for this Thread