By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,411 Members | 991 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,411 IT Pros & Developers. It's quick & easy.

function typedefs

P: n/a
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?
Jul 1 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Graham Lee wrote:
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.

--
Ian Collins.
Jul 1 '06 #2

P: n/a
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 1 '06 #3

P: n/a
Graham Lee wrote:
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

Jul 1 '06 #4

P: n/a
lovecreatesbeauty wrote:
Graham Lee wrote:
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.

What do you mean 'not a good idea'? It's plain (as in won't compile) wrong.

--
Ian Collins.
Jul 1 '06 #5

P: n/a
Graham Lee wrote:
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
Jul 1 '06 #6

P: n/a
On Sat, 01 Jul 2006 01:52:03 +0100, Graham Lee
<uk***********************@leeg.invalidwrote:
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
Jul 10 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.