0

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;
Lewis994
  • 11
  • 3
  • C doesn't fit such expectation. – Alex Apr 15 '15 at 14:02
  • Why every one knows what he is asking for? His code snippets are all made of valid syntax, but I really don't know what he wants to know, what he is expecting to happen, and what is wrong with his diea precisely. – dhein Apr 15 '15 at 14:36
  • Zaibis, it sounds like he's asking for something like javascript's ability to iterate through all the properties of an object as in http://stackoverflow.com/questions/684672/loop-through-javascript-object – John Hascall Apr 15 '15 at 17:30

4 Answers4

3

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.

unwind
  • 391,730
  • 64
  • 469
  • 606
1

This is not possible in C. After compilation, each variable is referenced by memory address only and no variable names are stored.

Shashwat Kumar
  • 5,159
  • 2
  • 30
  • 66
0

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);
 }
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.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82