Search code examples
cscopefunction-declaration

Variable looks like a function pointer


char *getoccupier(int roomno)
{
    //...
}

int main()
{
    int j;
    char *getoccupier(int), *p;

    for (j = 1; j <= NROOMS; j++)
    {
        if (p == getoccupier(j))
        {
            //...
        }
    }
}

In the main function I saw

*getoccupier(int)

variable.

I thought it was function pointer but I don't think it is.

Function pointer needs "()" like (*getoccupier)(int) but it doesn't have it.

What is that?


Solution

  • For starters its seems you mean

    if ( ( p = getoccupier(j) )){
    

    or

    if ( ( p = getoccupier(j) ) != NULL ){
    

    instead of

    if (p == getoccupier(j)){
    

    Within the function main

    char *getoccupier(int), *p;
    

    there is a block scope function declaration of the function getoccupier.

    The function declaration has external linkage though the linkage specifier extern is absent. So you may have several declarations of the same function.

    Pay attention to that parameter names may be omitted in a function declaration that is not at the same time the function definition.

    Also in one declaration you may declare several declarators as for example

    char c, *s, f( int );
    

    where there are declared an object c of the type char, the pointer s of the type char * and the function f of the type char( int ).

    Here is a demonstrative program.

    #include <stdio.h>
    
    void hello( void ) { puts( "Hello"); }
    
    int main(void) 
    {
        hello();
        
        int hello = 10;
    
        printf( "Hello = %d\n", hello );
    
        {   
            void hello( void );
        
            hello();
        }
        
        printf( "Hello = %d\n", hello );
    
        return 0;
    }
    

    Its output is

    Hello
    Hello = 10
    Hello
    Hello = 10