简体   繁体   中英

How to access the elements of a structure using variables with the exact name of the elements?

I'm not sure if this is at least possible, but I want to access the elements from my structure using a variable with the exact name of the element.

My program does operations with the members of the structure, and it is depending on the column that the user chooses, but to make it simpler (because that's not the main point) I'm going to write some simple lines, so it would be something like this:

(Let's say I've already filled out the list with another function, but that's not the main point neither, so I won't put it)

The structure, which is going to be used for each one of the nodes of a doubly linked list (each node represents like a row from a table with two columns)

typedef struct row {
    float A, B;
    struct row *prev, *next;
} _row;

Main

int main(){
    char column;
    printf("Which column would you like to see? (A or B):  ");
    scanf("%c",&column);
    show_column(column);
    system("PAUSE");
}

And the function

void show_column(char column){
    _row *aux;
    aux=start;
    while(aux->next!=NULL){
        printf("\n %.2f",aux->column);
        aux=aux->next;
    }
    //this is because that cycle is not going to show the last node
    printf("\n %.2f",aux->column);
}

"start" is a _row too. It is modified to point the start from the list in the same function where I insert the nodes.

What I want to know is how to make this part in the function:

printf("\n %.2f",aux->column);

Because "column" is not a member from the structure, but it is a variable that should contain the name of one of the members (A or B).

I need this so I won't have to repeat the same code but using "if" and with just a letter (B) different.

Sorry if there's something wrong with the orthography and grammar, my English is not very good, and thanks a lot for your help!

C is not interpreted but compiled language. As far as I know it is not possible. You'll have to write additional code. Besides, it is not nice for a simple user to know the names of your structure fields - so, don't worry =).

so basically the user gives you 'A', and you want column A.
if its just a case of single characters then you can just use the int behind the char
making your floats an array like this:

typedef struct row {
float col[2];    //A, B
struct row *prev, *next;
} _row;

then changing

printf("\n %.2f",aux->column);

to become

printf("\n %.2f",aux->col[(int(toUpper(column) - 'A'))]);

so 'A' will become 0,
'B' will become 1, etc.
this will only work for single character column names, so if you want a column "AA" this wont work without some modification.

You could do printf("\\n %.2f",(*(aux->column) == 'A' ? aux->A : aux->B)); I don't see how you can do this in another way than an if statement.

Maybe you can do this:

typedef struct row {
    float col[2];
    struct row *prev, *next;
} _row;

int main(){
    char column;
    printf("Which column would you like to see? (A or B):  ");
    scanf("%c",&column);
    show_column(column);
    system("PAUSE");
}

void show_column(char column){
    _row *aux;
    aux=start;

    column = column - 'A';
    while(aux->next!=NULL){
        printf("\n %.2f",aux->col[column]);
        aux=aux->next;
    }
    //this is because that cycle is not going to show the last node
    printf("\n %.2f",aux->col[column]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM