Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Code in C

Tags:

c

dynamic

I have the following codeblock I'm using for German verb drills:

if (strcmp(*option, "sein") == 0)
    *option = linie.sein;

if (strcmp(*option, "haben") == 0)
    *option = linie.haben;

if (strcmp(*option, "possessiv") == 0)
    *option = linie.possessiv;

if (strcmp(*option, "reflexiv") == 0)
    *option = linie.reflexiv;

if (strcmp(*option, "accusativ") == 0)
    *option = linie.accusativ;

if (strcmp(*option, "dativ") == 0)
    *option = linie.dativ;

However I would like to condense it to something like:

*option = linie.(*option);

Or perhaps:

*option = linie.(*option)();

Unfortunately neither of these work. Any ideas?

Edit @dasblinkenlight:

typedef struct
{
    char subjekt[20];
    char sein[20];
    char haben[20];
    char possessiv[20];
    char reflexiv[20];
    char accusativ[20];
    char dativ[20];
} satz;

satz linie =
{
    .subjekt = "",
    .sein = "",
    .haben = "",
    .possessiv = "",
    .reflexiv = "",
    .accusativ = "",
    .dativ = ""
};

char *option = argv[1];
like image 665
user1408643 Avatar asked Nov 10 '12 14:11

user1408643


People also ask

What is dynamic programming in C?

Dynamic Programming is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of subproblems, so that we do not have to re-compute them when needed later.

Can we use dynamic programming in C?

Dynamic programming approach was developed by Richard Bellman in 1940s. It was an attempt to create the best solution for some class of optimization problems, in which we find a best solution from smaller sub problems.

What is dynamic and static in C?

When the allocation of memory performs at the compile time, then it is known as static memory. When the memory allocation is done at the execution or run time, then it is called dynamic memory allocation. 2. The memory is allocated at the compile time. The memory is allocated at the runtime.


1 Answers

Kirilenko's answer is a good one and works well for short structures like yours. However, for longer structures, maintaining all of the strcmp calls can be cumbersome. To solve this, you can define a relationship between the keyword you are trying to match and the offset of the corresponding element in your structure.

struct relation
{
    char keyword[20];
    int offset;
};

Then you can use the offsetof macro (in stddef.h) to link the keyword to its position in your structure.

#define REL_LEN (7)

struct relation rel[REL_LEN] = {
    {"subjekt", offsetof(satz, subjekt) },
    {"sein", offsetof(satz, sein) },
    {"haben", offsetof(satz, haben) },
    {"possessiv", offsetof(satz, possessiv) },
    {"reflexiv", offsetof(satz, reflexiv) },
    {"accusativ", offsetof(satz, accusativ) },
    {"dativ", offsetof(satz, dativ) }
};

Finally, the function to retrieve your character string using the mapping above could look something like this.

char *lookup_keyword(const satz *linie, const char *option, 
                     const struct relation *rel, size_t rel_size)
{
    int i;
    char *pchar = (char *)linie;

    for (i=0; i<rel_size; i++)
    {
        if (strcmp(option, rel->keyword) == 0)
        {
            pchar += rel->offset;
            return pchar;
        }
        rel++;
    }
    printf("Error: no mapping found matching %s!\n", option);
    return "";
}

And you can invoke it like so

char *option = argv[1];

printf("Result for %s: %s\n", option, 
       lookup_keyword(&linie, option, rel, REL_LEN));
like image 140
dorfex Avatar answered Oct 05 '22 10:10

dorfex