0

Porting C Code to C++ code. This C Code was written some 20 years back for Solaris.

CHAR *(*MallocPtr)(); //Declared Globally

//inside function
if (!(PTablePtr = (PType *)(*MallocPtr)(sizeof(PType) + (numCharacters * sizeof(CharInfo)))))
        return PCODE;

In the above declaration this MallocPtr is not accepting any params. But its calling with params. Function declaration and all was using K&R not ANSI C. This code is compiling successfully in solaris from the beginning.. Am trying to port to CPP using visual studio. I know it looks absurd. but am really clueless whats going on. This is one such example. But there were many more functions in similar way like declaration will not have params but when calling its called with arguements. Functions are defined with params only. Function without param is not defined anywhere in code.

Casey
  • 10,297
  • 11
  • 59
  • 88

1 Answers1

1

This declaration in C

CHAR *(*MallocPtr)();

means that the function can have any number or types of parameters. In C++ such a declaration means that the function has no parameters. Equivalently to the meaning of the declaration in C++ the function pointer in C can look like

CHAR *(*MallocPtr)( void );

From the C Standard (6.7.6.3 Function declarators (including prototypes))

14 An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335