function typedefs

G

Graham Lee

Given a typedef like this:

typedef double LoadsOfArgs(double arg1, double arg2, double arg3, double
arg4);

I can *declare* a function using the typedef:

LoadsOfArgs geoMean;

int main(void)
{
printf("G.M.(1,2,3,4) = %g\n",geoMean(1.0,2.0,3.0,4.0));
exit(EXIT_SUCCESS);
}

but then don't appear to be able to *define* the function in the same way:

LoadsOfArgs geoMean
{
return pow(arg1*arg2*arg3*arg4,0.25);
}

is use of the full function prototype mandated, or is my function
definition wrong?
 
I

Ian Collins

Graham said:
Given a typedef like this:

typedef double LoadsOfArgs(double arg1, double arg2, double arg3, double
arg4);
The parameter names are not part of the type, only the parameter types.
I can *declare* a function using the typedef:
Correct;

but then don't appear to be able to *define* the function in the same way:
Correct.

is use of the full function prototype mandated, or is my function
definition wrong?

As I said, the parameter names are not part of the type, so you can use
the typedef in this way.
 
R

Richard Heathfield

Graham Lee said:
Given a typedef like this:

typedef double LoadsOfArgs(double arg1, double arg2, double arg3, double
arg4);

I can *declare* a function using the typedef:

LoadsOfArgs geoMean;

Yes. I do that too.
but then don't appear to be able to *define* the function in the same way:

LoadsOfArgs geoMean

I, too, was a bit annoyed by that, but c'est la vie.
is use of the full function prototype mandated, or is my function
definition wrong?

Alas, you have to define the function in longhand. Grrrr.
 
L

lovecreatesbeauty

Graham said:
typedef double LoadsOfArgs(double arg1, double arg2, double arg3, double
arg4);

LoadsOfArgs geoMean;

LoadsOfArgs geoMean
wrong

{
return pow(arg1*arg2*arg3*arg4,0.25);
}

This is not a good idea. The prototype can not be directly shown as you
define (compose) the function in a .c file while the function prototype
maybe is placed in another .h file.

lovecreatesbeauty
 
I

Ian Collins

lovecreatesbeauty said:
This is not a good idea. The prototype can not be directly shown as you
define (compose) the function in a .c file while the function prototype
maybe is placed in another .h file.
What do you mean 'not a good idea'? It's plain (as in won't compile) wrong.
 
A

August Karlstrom

Graham said:
Given a typedef like this:

typedef double LoadsOfArgs(double arg1, double arg2, double arg3, double
arg4);

I can *declare* a function using the typedef:

LoadsOfArgs geoMean;

int main(void)
{
printf("G.M.(1,2,3,4) = %g\n",geoMean(1.0,2.0,3.0,4.0));
exit(EXIT_SUCCESS);
}

but then don't appear to be able to *define* the function in the same way:

LoadsOfArgs geoMean
{
return pow(arg1*arg2*arg3*arg4,0.25);
}

is use of the full function prototype mandated, or is my function
definition wrong?

You probably want something like

#include <stdio.h>
#include <math.h>

typedef double LoadsOfArgs(double a, double b, double c, double d);

double f(double a, double b, double c, double d)
{
return pow(a * b * c * d, 0.25);
}

LoadsOfArgs *geoMean = f;

int main(void)
{
printf("G.M.(1,2,3,4) = %g\n", geoMean(1.0, 2.0, 3.0, 4.0));
return 0;
}


August
 
D

Dave Thompson

I can *declare* a function using the typedef:
but then don't appear to be able to *define* the function in the same way:
is use of the full function prototype mandated, or is my function
definition wrong?

Nonuse of the typedef is indeed mandated; C90 6.7.1 or C99 6.9.1p2.

You must use either the longhand prototype or the equally longhand
oldstyle definition (although this is labelled 'obsolescent') e.g.
double LoadsOfArgs (a,b,c,d)
double a,b,c,d;
{ body }

You _could_ do (at least the argument portion of) either of these as a
macro, and for the prototype case could reuse that macro in the
typedef. Personally I can't decide whether that's cute or disgusting.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top