A function which doesn't function

This is a discussion on A function which doesn't function within the C Programming forums, part of the General Programming Boards category; I have started to write a program which will manage the queue of a clinic. The program is quit big, ...

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

    A function which doesn't function

    I have started to write a program which will manage the queue of a clinic. The program is quit big, so I'm sending only the problematic core I'm facing now, which is ~80 lines longÖ Please bare with me a minute.
    The thing is when Iím running it, InsertPatient function is slipping away & doesnít come up to user interface.
    Code:
    #include <stdio.h>
    #include <malloc.h>
    
    
    struct PatientDetails {
    	char F_name[15];
    	char L_name[20];
    };
    
    typedef struct PatientDetails Patient;
    Patient CurrentPatient;
    
    struct LinkedList
    {
    	Patient Data;
    	struct LinkedList *Next;
    };
    
    typedef struct LinkedList Node;
    typedef Node *PtrToNode;
    	
    
    Patient AbsrbPatient(Patient *PatientPtr)
    {
    	
    	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);
    	return *PatientPtr;
    }
    
    PtrToNode InitList()
    {
    	PtrToNode P;
    	P = (PtrToNode) malloc(sizeof(Node));
    	if (P == NULL)
    		 puts ("Out of memory!");
    	P->Next = NULL;
    	return P;
    }
    
    void InsertPatient(PtrToNode PatientPtr, PtrToNode list)
    {
    	PtrToNode TmpCell;
    
    	TmpCell=(PtrToNode)malloc(sizeof(Node));
    	if (TmpCell == NULL)
    		 printf("Out of memory!\n");
    	else
    	{
    	     list->Data = PatientPtr->Data;
    	     if (PatientPtr == NULL) 
    	     {
    		TmpCell->Next = TmpCell;
    		PatientPtr = TmpCell;
    	      }
    	      else
    	      {
    		TmpCell->Next = PatientPtr;
    		PatientPtr = TmpCell;
    	      }
    	 }
    }
    
    int main(void)
    {
    	Patient *PatientPtr = &CurrentPatient;
    
    	PtrToNode UrgentList, RegularList;
    	UrgentList=InitList();
    	RegularList=InitList();
    
    	AbsrbPatient(PatientPtr);
    	InsertPatient(PatientPtr, UrgentList);
    return 1;
    }

    Debugger shows:
    warning C4133: 'function' : incompatible types - from 'struct PatientDetails *' to 'struct LinkedList *'
    I cannot seem to find why it complains of incompatible types!?
    Please help me find it.

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    These warnings are expected behavior. According to ANSI standards, variables of type int* are different from variables of type short*. And, although the compiler may be able to perform an implicit promotion/demotion among intergral types (char, short, int, long, etc.), pointers are not of integral type.

    To remove these warnings, use an appropriate cast to instruct the compiler to explicitly convert the value from its current data type to the desired.

    The only restriction on the size of the short given by the ANSI standard is that it NOT be longer than an integer. Code should be written with this in mind so that it will be portable.

    Compiling the following program will generate the discussed warning:
    Code:
    void ShortFunc( short, short * );
    void Func( int, int * );
    
    int main() {
    	short sTest;
    	short *sPoint;
    
    	ShortFunc(sTest, sPoint);
    	Func(sTest, sPoint);		// Generates warning
    	Func(sTest, (int *)sPoint);	// Does not generate warning
    
    	return 0;
    }
    So for example, let's disect your code. Firstly, there is a LinkedList and a Patient struct. PtrToNode is a pointer to a LinkedList, and Patient is a declaration of PatientDetails.

    Now InsertPatient() takes two parameters, both PtrToNode. Though, in main() you call InsertPatient() with the first parameter of Patient while it should be a PtrToNode variable.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    So how do I solve this? How do I do cast in this situation? Or do I have to call Patient type in this function?
    Last edited by ronenk; 12-01-2004 at 06:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM

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