search - Get property dynamically in c -


i'm new c. i'm building tiny "database" while i'm learning. undestand of basics in c, i'm having trouble pointers , arrays.

i create simple key-value find function. key entered user argument (argv) when program executed.

i'm stuck @ printing specific dynamic columns of row. this:

myarray[i][key] // key dynamic, loop 

here find function:

void find(char *key, char *value){     // print intention terminal     printf("\nget %s=%s\n", key, value);      // rows     struct address *rows = conn->db->rows;      // list rows     int = 0;     for(i = 0; < 100; i++){         if(rows[i].set){             printf("rows[%d] = %s\n", i, rows[i][key] ); // <-- how this?         }     } } 

here address structure:

struct address {     int id;     int set;     char name[512];      char email[512]; }; 

and here database structure:

struct database {     struct address rows[100]; }; 

this close can get:

// have return void*, because don't know type expect void* getfield(struct address* addr, char* field) {     if(strcmp(field, "name") == 0) return &addr->name;     if(strcmp(field, "id") == 0) return &addr->id;     if(strcmp(field, "email") == 0) return &addr->email;     return 0; } 

but doesn't as might hope, because:

char* key = "name"; void* value = getfield(&row, key); // uh oh - don't know type `value` points to. better double check:  //               changes per field   v              v  , if(value == &row.name)  printf("row.%s = %s\n", key, *(char(*)[512])value); if(value == &row.email) printf("row.%s = %s\n", key, *(char(*)[512])value); if(value == &row.id)    printf("row.%s = %d\n", key, *(int*)        value);  // conceivably write function return correct format specifier, // language not allow write function appropriate cast 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -