[comp.lang.c] More problems with passing functions as arguments.

pm0@reef.cis.ufl.edu (Patrick Martin) (03/21/91)

void Function(i)

int i;

{
   printf("\nThe Number Is: %d\n",i);
}

void Pass_Me_A_Function(F)

void (*F) (int);

{
   F(1);
}

main()

{
   Pass_Me_A_Function(Function);
}


This code works fine when compiled with gcc or my compiler at home.
When compiled with some of the older implementations of C it fails.

Is there a way I can get this to run with some of the older versions
of C?

PS: It fails on the declaration of F in Pass_Me_A_Function of
void (*F) (int);

Thanks for the help,
Pat

jerry@matt.ksu.ksu.edu (Jerry J. Anderson) (03/21/91)

In article <27546@uflorida.cis.ufl.EDU> pm0@reef.cis.ufl.edu (Patrick Martin) writes:

[original code deleted]

>This code works fine when compiled with gcc or my compiler at home.
>When compiled with some of the older implementations of C it fails.
>
>Is there a way I can get this to run with some of the older versions
>of C?

The following compiles under either gcc or cc (non-ANSI).

    void function(i)
        int i;
    {
        printf("\nThe Number Is: %d\n\n",i);
    }
    
    pass_me_a_function(f)
        void (*f)();
    {
        f(1);
    }
    
    main()
    {
        pass_me_a_function(function);
    }

--
"The size of an array is the size of an           Jerry J. Anderson
array; the size of a pointer is the size          Computing Activities
of a pointer."  - Tim Ramsey, under the           Kansas State University
influence of little blue pills.                   Manhattan  KS  66506

mouse@thunder.mcrcim.mcgill.edu (der Mouse) (03/23/91)

In article <27546@uflorida.cis.ufl.EDU>, pm0@reef.cis.ufl.edu (Patrick Martin) writes:
[code compressed to save lines -dM]
> void Function(i)
> int i;
> { printf("\nThe Number Is: %d\n",i); }

> void Pass_Me_A_Function(F)
> void (*F) (int);
> { F(1); }

> main()
> { Pass_Me_A_Function(Function); }

> This code works fine when compiled with gcc or my compiler at home.
> When compiled with some of the older implementations of C it fails.

> It fails on the declaration of F in Pass_Me_A_Function of
> void (*F) (int);

The "older" implementation probably does not understand prototypes.
Remove the argument type declaration; try just void (*F)(); and see if
it works an;y better.

With some, you may find you also have to write the call as (*F)(1);.
There are no compilers I am sure this is necessary for, but I have seen
only a tiny fraction of all compilers in existence :-)

In passing, why do you prototype the argument to Pass_Me_A_Function but
not anything else?  I would argue on stylistic grounds that Function
should be prototyped to match the argument to Pass_Me_A_Function,
though I think the given prototype is compatible with the given
old-style declaration.

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu