简体   繁体   中英

Can you access Members of a Structure using for loop?

Is there a way to access members of a struct using a for loop? My program runs multiple commands for each member. The commands are identical except that the member is being changed each time.

struct data
{
    int recordID;
    int idNumber;
    char firstName[100];
    char lastName[100];
    int dateOfBirth;
    char grade[2];
};

#define ACCOUNTS 100
struct data rec[ACCOUNTS];

I was hoping for something like

const char *FIELDID[][6] =
{
    {"recordID", "idNumber", "firstName", "lastName", "dateOfBirth", "grade"},
{"something else", "something else", "something else", "something else", "something else", "something else"},
{"something else", "something else", "something else", "something else", "something else", "something else"}
};

and call

rec[count].FEILDID[0][0] = value;

No, there's no such support built into C.

Realize that the actual code required to "set" a value varies greatly with the type of the value to be set and that variable names do not exist at runtime.

It can be done by adding the necessary meta data yourself, but it's not going to be very easy and of course will require maintenance when/if the structure changes.

这在C语言中是不可能的。编译后,每个变量仅由内存地址引用,并且不存储任何变量名。

Hope this code answer to your question......if I understood your problem correctly

#include<stdio.h>
#include<string.h>
typedef struct Vehicle
{
  int wheels;
  char vname[20];
  char color[10];  
}Vehicles;

int main(){
    Vehicles v[2];
    v[0].wheels = 4;
    strcpy(v[0].vname, "dsvdf");
    strcpy(v[0].color, "bfdvsidvbi");
    v[1].wheels = 4;
    strcpy(v[1].vname, "dsvdf");
    strcpy(v[1].color, "bfdvvfbdfsidvbi"); 
    int i;
    for ( i = 0; i < 2; ++i){
     printf("Vehicle No of Wheels : %d\n",v[i].wheels);
     printf("Vehicle Name           : %s\n",v[i].vname) ;
     printf("Vehicle Color          : %s\n",v[i].color)
    }
    return(0);
 }

You can do it by using function pointers. Since C is strongly typed, you would still have to handle fields of different type separately, though.

Example:

/* setInt_type is a type for function pointers to a function that takes a struct data*, and an int as parameters and returns int */
typedef int (*)(struct data*, int) setInt_type; 

/* Setter function, need one for each int field */
int setAccountIdNumber(struct data *account, int newValue) {
    return account->idNumber = newValue;
}
int setAccountDateOfBirth(struct data *account, int newValue) {
    return account->dateOfBirth = newValue;
}

/* Function that can operate on any int field */
int setAccountIntField(struct data *account, int newValue, setInt_type setField) {
    setField(account, newValue);
}

Then you can create an array of function pointers:

const setInt_type INT_FIELDS[] = { setAccountIdNumber, setAccountDateOfBirth }

And finally use it all in a program:

int main() {
    setAccountIntField(&rec[3], 43, setAccountIdNumber); // Assigns 43 to rec[3].idNumber

    setAccountIntField(&rec[3], 43, setAccountDateOfBirth); // Assigns 43 to rec[3].dateOfBirth

    // or, using INT_FIELDS:
    setAccountIntField(&rec[3], 43, INT_FIELDS[1]); // Assigns 43 to rec[3].dateOfBirth

    return 0
}

As you can see, it isn't quite as neat as one would like.

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