Question

I have three structures in one .h file.

typedef struct name{
      char first[20];
      char middle;
      char last[20];
    } PersonName;

typedef struct address{
      char street[20];
      char city[20];
      char state[20];
      int zipCode;
        } PersonAddress;

typedef struct employee{
      PersonName bob;
      PersonAddress place;
      int id;
    } Employee;

And what I'm trying to do is use the employee structure to do the work of the other structures.

I have a call:

     void setName(struct employee* object, char* f, char* l, char* m);

to my .c file which is supposed to receive this at:

 void setName(struct employee* object, char* f, char* l, char* m){
        strncpy(object.bob.first,f,20);
        strncpy(object.bob.last,l, 20);
        object.bob.middle=m[0];

    }

To be able to change the values without using the other structures. Am I just looking at it the wrong way? CMD says "Request for bob in something not a structure. Any helps would be greatly appreciated.

Was it helpful?

Solution

Bob is a pointer. In order to access its members, try using the -> operator instead of .

In other words:

void setName(struct employee* object, char* f, char* l, char* m){
  strncpy(object->bob.first,f,20);
  strncpy(object->bob.last,l, 20);
  object->bob.middle=m[0];
}

OTHER TIPS

object is a pointer, hence you need to type object->bob to access bob. bob is a struct, so you need object->bob.first to access first.

Also, you might want to replace all those 20 by a constant before you need to change them, forget to change one, and have bugs all over the place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top