Data structure error

This is a discussion on Data structure error within the C Programming forums, part of the General Programming Boards category; Got this code, which is part of a larger program, but in order to debug it I have separated it ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    Data structure error

    Got this code, which is part of a larger program, but in order to debug it I have separated it from the rest of it.
    Code:
    #include <stdio.h>
    struct PatientDetails {
    	char F_name[15];
    	char L_name[20];
    	int QueueType;
    };
    typedef struct PatientDetails Patient;
    	Patient CurrentPatient;
    	Patient *PatientPtr;
    	PatientPtr = &CurrentPatient;
    
    void AbsrbPatient()
    {
    	puts ("\nPlease type patient's first name");
    	flushall();
    	gets (PatientPtr->F_name);
    	printf ("\nfirst name is %s", PatientPtr->F_name); 
    	puts ("\nPlease type patient's last name");
    	flushall();
    	gets (PatientPtr->L_name);
    	printf ("\nlast name is %s", PatientPtr->L_name);
    }
    
    int main(void)
    {
    		AbsrbPatient();
    }
    Running it (also in original program) I get:

    error C2040: 'PatientPtr' : 'int ' differs in levels of indirection from 'struct PatientDetails *'
    warning C4047: 'initializing' : 'int ' differs in levels of indirection from 'struct PatientDetails *'
    Nevertheless, if I run the same code with a little modification it runs OK, but it doesn’t looks very nice…

    Code:
    #include <stdio.h>
    struct PatientDetails {
    	char F_name[15];
    	char L_name[20];
    	int QueueType;
    };
    typedef struct PatientDetails Patient;
    	
    
    void AbsrbPatient()
    {
    	Patient CurrentPatient;
    	Patient *PatientPtr;
    	PatientPtr = &CurrentPatient;
    	puts ("\nPlease type patient's first name");
    	flushall();
    	gets (PatientPtr->F_name);
    	printf ("\nfirst name is %s", PatientPtr->F_name); 
    	puts ("\nPlease type patient's last name");
    	flushall();
    	gets (PatientPtr->L_name);
    	printf ("\nlast name is %s", PatientPtr->L_name);
    }
    
    int main(void)
    {
    	Patient CurrentPatient;
    	Patient *PatientPtr;
    	PatientPtr = &CurrentPatient;
    	AbsrbPatient();
    }
    How should I right the upper version better ?

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    A few things, your main doesn't return anything, it should return an exit code. Also, you should avoid globals, which is what the first code uses. You should really declare them in main and pass what is needed to the function as an argument.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >How should I right the upper version better ?

    Code:
    	Patient *PatientPtr = &CurrentPatient;
    This is an initialization. What you had was a declaration followed by an assignment. You cannot have an assignment outside of a function.

    FAQ > Explanations of... > Why gets() is bad / Buffer Overflows
    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.*

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    Thank you, chrismiceli & Dave_Sinkula. I'm all set with your notes.
    Last edited by ronenk; 11-30-2004 at 08:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21