making pointers to structure elements using array elements 
Author Message
 making pointers to structure elements using array elements

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;

Quote:
};

char elements[3][6]={{"Name"},{"Email"},{"Phone"}};    /* used to
format calls for the purposes of learning this concept */

{
...

Quote:
}

void add_contact(struct contacts *current_contact)
{
  int counter;
   for (counter=0;counter <3;counter++) { /* counter used for now
since I'm lazy and don't want to write array length stuff yet */
     printf("\n%s of contact:  ",*(elements+counter)); /* this works
as intended */

*note*  current_contact->(how ever I manage to convert the contents
that elements+counter points to to describe the pointer)

 scanf("%s",current_contact->elements+counter);     /* I REALLY wish
the documentation I found for fgets and fscanf wasn't so freaking
obfuscated
manybe someone should try writing it in english sometime.
 */
   }



Sun, 05 Sep 2004 12:49:17 GMT  
 making pointers to structure elements using array elements

Quote:
> 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;

Quote:
};

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;

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

Quote:
}

--

| 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


Sun, 05 Sep 2004 12:57:57 GMT  
 making pointers to structure elements using array elements



Quote:

> > 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()?
I'm fairly sure that's what they put it in
stddef.h for.

- Show quoted text -

Quote:

> 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



Mon, 06 Sep 2004 02:24:41 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Acces the 11th element of a 10 element array

2. checkout compilers (Was: Acces the 11th element of a 10 element array)

3. pointers to an array and accesing elements of the array

4. conversion of pointer to array to pointer to its element

5. Setting structure array elements

6. Getting address of element of structure array.

7. Setting elements in a structure array

8. float element in a structure array

9. C Beginner: Initialize a structure variable's array element

10. Question about fgets and array structure elements

11. updating structure elements from a pointer passed to the function

12. Passing pointers to elements of a structure.

 

 
Powered by phpBB® Forum Software