> > I wanted to know, and I'm sure there is a way, to build pointer names
> > using the contents of an array available to a function. I guess my
> > real question is this:
> > How to I get a character representation from a pointer, such as
> > *pointer="name"; appended to another pointer name like
> > current_contact->(the appended name here)
> > I'd love to make a function that takes a structure pointer and an
> > array pointer and updates all the elements of a linked list using just
> > the pointer of an array to specify the elements.
> > the following is some of the code.
> > struct contacts { /* keeps basic contact info and is a
> > linked list */
> > char name[50];
> > char email[50];
> > char phone[50];
> > struct contacts *next;
> > };
> > char elements[3][6]={{"Name"},{"Email"},{"Phone"}}; /* used to
> > format calls for the purposes of learning this concept */
> What you ask for cannot be done in C. In C, the names of the structure
> fields only exist during before compilation, in the produced executable
> they are replaced by raw addresses.
> You will have to simulate it by having a look-up table from strings to
> addresses, something like this:
> struct contacts {
> char name[50];
> char email[50];
> char phone[50];
> struct contacts *next;
> };
> char elements[3][6]={"Name", "Email", "Phone"};
> int offsets[3];
> struct contacts mycontacts;
> offsets[0]=&mycontacts.name - &mycontacts;
> offsets[1]=&mycontacts.email - &mycontacts;
> offsets[2]=&mycontacts.phone - &mycontacts;
do you hate offsetof()? > Then when you get the name of a field in a string you will have to
> look each of these fields up like this:
> struct contacts newcontacts;
> char s[20];
> int i;
> void *p=NULL;
> printf("Enter field name: ");
> fflush(stdout);
> fgets(s, 20, stdin);
> for (i=0; i<3; i++)
> if (!strcmp(elements[i], s))
> p=&newcontacts + offsets[i];
> if (p!=NULL) {
> printf("Enter field value: ");
> fflush(stdout);
> fgets(p, 50, stdin);
> }
> --
> | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
> | http://www.helsinki.fi/~palaste W++ B OP+ |
> \----------------------------------------- Finland rules! ------------/
> "You have moved your mouse, for these changes to take effect you must shut
down
> and restart your computer. Do you want to restart your computer now?"
> - Karri Kalpio