Thread: Help needed with structures

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    Help needed with structures

    I'm beggining to work on a project for school which consists in creating a program for managing the pacients and doctors of a clinic.Doctors would have their personal data(name,age,address,speciality,number of pacients treated,etc.) and pacients would have(name,age,adress,number of visits to the clinic,date of visits to the clinic,etc.)
    After searching and reading a bit about structures,still I'm not very clear with them in terms of data variables or organization.
    First,I thought on organizing the structures as following,by creating two structures:

    Code:
    struct Doctors
       {
           char speciality[15];
           int nptreated;
       };
    
    struct Pacients
       {
           int nvisits;
       };
    But then I thought,why would I repeat the variables name and age and address if both Pacients and Doctors use themand instead add them in another structure?So I thought in creating another structure that could hold those variables making them more generic:

    Code:
    struct Person
       {
           char name[20];
           int   age;
           char address[50];
           struct Pacients p;
          struct Doctors d;
       };
    So,in spite of thinking that the later solution looks best,what is your opinion?Anything that could help me clear this out and the overall concept of structures would be helpful!Thanks in advance!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I would probably tend to look at it like this:
    Code:
    struct Info
    {
       char name[20];
       int  age;
       char address[50];
    };
    
    struct Doctor
    {
       struct Info info;
       char speciality[15];
       int nptreated;
    };
    
    struct Patient
    {
       struct Info info;
       int nvisits;
    };
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can certainly do structs inside structs -- but I think you have the containing backwards. You can certainly create a struct Personal_Data or such, to hold name, age, and address; but then you would need to have that struct inside your patient struct and your doctor struct. (IOW, you don't want a person to be both a doctor and a patient; but a patient or a doctor is a person.)

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Thanks for the reply!I guess I really had the containing backwards!
    So,basically can we look at structures like we look at classes?Like,for example, ask "questions" like Person IS-A Doctor ,and if it fails don't put it within the structure?

  5. #5
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by esmeco View Post
    Thanks for the reply!I guess I really had the containing backwards!
    So,basically can we look at structures like we look at classes?Like,for example, ask "questions" like Person IS-A Doctor ,and if it fails don't put it within the structure?
    If you have a struct named Person, and both Doctor and Patient have it as their first member, you can do things like faking "upcasting" by using a Doctor or a Patient, as a Person with an explicit cast, but since C doesn't understand the notion of RTTI you can not ask a Person struct it's exact type. You can recast it back to a Doctor, or a Patient struct given the fact that you know what it originally was.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You can recast it back to a Doctor, or a Patient struct given the fact that you know what it originally was.
    Or you can add some function pointers to the person struct - and overwrite them for doctor and patient - emulating the C++ virtual function mechanism - removing by that the need to upcast
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by vart View Post
    Or you can add some function pointers to the person struct - and overwrite them for doctor and patient - emulating the C++ virtual function mechanism - removing by that the need to upcast
    True. And that might be an indication that one should switch to a language supporting OOP constructs. I recall there is a whole MACRO'ed Object Oriented C package somewhere out there, which let's you use some weird syntax to even do much more than polymorphism.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The Microsoft way of doing it is to have an enum or int inside the struct that corresponds to the type. So you would have one struct of, say, person, like this:

    Code:
    struct person
    {
       struct Info info;
       int type;
       void *obj;
    };
    type would be one of the following defines perhaps:

    Code:
    #define DOCTOR 0
    #define PATIENT 1
    enums would obviously be a better choice for this, but you get the idea. The void * named obj would be a pointer to a doctor or patient struct. Naturally, you need to cast this point to the appropriate type, but you can do it relatively safely based upon the type variable.

    In that respect you can have a simple OO approach. A doctor and a patient are both a person.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Replies: 3
    Last Post: 11-14-2005, 08:03 AM
  4. sorting union of structures with q sort
    By colw in forum C Programming
    Replies: 6
    Last Post: 04-09-2002, 09:51 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM