4
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?

anastaciu
  • 23,467
  • 7
  • 28
  • 53
evergreen
  • 681
  • 10
  • 22

2 Answers2

2

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
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

char *getoccupier(int), *p; declares getoccupier to be a function taking an int argument and returning a pointer to a char (a char *), and it declares p to be a pointer to a char.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312