Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   how to define a function pointer variable witout typdef? (http://www.velocityreviews.com/forums/t438164-how-to-define-a-function-pointer-variable-witout-typdef.html)

baumann@pan 05-27-2005 01:53 AM

how to define a function pointer variable witout typdef?
 
hi all,

typedef int (*pfunc)(int , int);
pfunc a_func;

i know it's ok,

but how can define a_func without typedef statement?

thanks .

baumann@pan


Robert Gamble 05-27-2005 02:02 AM

Re: how to define a function pointer variable witout typdef?
 
baumann@pan wrote:
> hi all,
>
> typedef int (*pfunc)(int , int);
> pfunc a_func;
>
> i know it's ok,
>
> but how can define a_func without typedef statement?


int (*a_func)(int, int);

Rob Gamble


Martin Ambuhl 05-27-2005 02:04 AM

Re: how to define a function pointer variable witout typdef?
 
baumann@pan wrote:
> hi all,
>
> typedef int (*pfunc)(int , int);
> pfunc a_func;
>
> i know it's ok,
>
> but how can define a_func without typedef statement?
>


int (*a_func)(int, int);

cdecl <<EOD
explain int (*a_func)(int, int)
quit
EOD
declare a_func as pointer to function (int, int) returning int

sathyashrayan 05-27-2005 04:28 PM

Re: how to define a function pointer variable witout typdef?
 

"baumann@pan" <baumann.Pan@gmail.com> wrote in message news:1117158824.201053.225240@o13g2000cwo.googlegr oups.com...
> hi all,
>
> typedef int (*pfunc)(int , int);
> pfunc a_func;
>
> i know it's ok,
>
> but how can define a_func without typedef statement?
>
> thanks .
>


int (*a_func)(int, int);
But this is a declaration not a definition.


--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com




Jens.Toerring@physik.fu-berlin.de 05-27-2005 05:03 PM

Re: how to define a function pointer variable witout typdef?
 
sathyashrayan <sathyashrayan@removethisgmail.com> wrote:

> "baumann@pan" <baumann.Pan@gmail.com> wrote in message news:1117158824.201053.225240@o13g2000cwo.googlegr oups.com...
>> typedef int (*pfunc)(int , int);
>> pfunc a_func;
>>
>> i know it's ok,
>>
>> but how can define a_func without typedef statement?


> int (*a_func)(int, int);
> But this is a declaration not a definition.


No, this creates a variable, named 'a_func', that can hold a pointer
to a function taking two int arguments and returning an int. No
'extern' to see seen in front of it, so it's a definition, not a
declaration.
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de

CBFalconer 05-27-2005 07:10 PM

Re: how to define a function pointer variable witout typdef?
 
Jens.Toerring@physik.fu-berlin.de wrote:
> sathyashrayan <sathyashrayan@removethisgmail.com> wrote:
>> "baumann@pan" <baumann.Pan@gmail.com> wrote in message

>
>>> typedef int (*pfunc)(int , int);
>>> pfunc a_func;
>>>
>>> i know it's ok,
>>>
>>> but how can define a_func without typedef statement?

>
>> int (*a_func)(int, int);
>> But this is a declaration not a definition.

>
> No, this creates a variable, named 'a_func', that can hold a
> pointer to a function taking two int arguments and returning an
> int. No 'extern' to see seen in front of it, so it's a definition,
> not a declaration.


It's a definition of a pointer variable, which in turn is useless
until filled with a pointer to a compatible function. Such a
function can be defined with:

int this_func(int a, int b)
{
/* the actual definition goes here */
}

and then the statement:

a_func = this_func;

can initialize a_func, and make it usable. Because there is no '('
immediately following a_func or this_func these are pointer values,
not function calls.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html


pete 05-27-2005 11:45 PM

Re: how to define a function pointer variable witout typdef?
 
Jens.Toerring@physik.fu-berlin.de wrote:
>
> sathyashrayan <sathyashrayan@removethisgmail.com> wrote:
>
> > "baumann@pan" <baumann.Pan@gmail.com> wrote in message
> > news:1117158824.201053.225240@o13g2000cwo.googlegr oups.com...
> >> typedef int (*pfunc)(int , int);
> >> pfunc a_func;
> >>
> >> i know it's ok,
> >>
> >> but how can define a_func without typedef statement?

>
> > int (*a_func)(int, int);
> > But this is a declaration not a definition.

>
> No, this creates a variable, named 'a_func', that can hold a pointer
> to a function taking two int arguments and returning an int. No
> 'extern' to see seen in front of it, so it's a definition, not a
> declaration.


It's a definition and a declaration.

--
pete

E. Robert Tisdale 05-28-2005 12:49 AM

Re: how to define a function pointer variable witout typdef?
 
baumann@pan wrote:

> typedef int (*pfunc)(int , int);
> pfunc a_func;
>
> I know it's ok,
> but how can I define a_func without typedef statement?


> cat main.c

#include <stdio.h>

int this_func(const int a, const int b) {
return fprintf(stdout, "a = %d\tb = %d\n", a, b);
}

int (*a_func)(int, int) = this_func;

int main(int argc, char* argv[]) {
a_func(13, 42);
return 0;
}

> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main

a = 13 b = 42

sathyashrayan 05-28-2005 02:07 AM

Re: how to define a function pointer variable witout typdef?
 

<Jens.Toerring@physik.fu-berlin.de> wrote in message news:3fp26aF8nda3U1@uni-berlin.de...
> sathyashrayan <sathyashrayan@removethisgmail.com> wrote:
>
> > "baumann@pan" <baumann.Pan@gmail.com> wrote in message news:1117158824.201053.225240@o13g2000cwo.googlegr oups.com...
> >> typedef int (*pfunc)(int , int);
> >> pfunc a_func;
> >>
> >> i know it's ok,
> >>
> >> but how can define a_func without typedef statement?

>
> > int (*a_func)(int, int);
> > But this is a declaration not a definition.

>
> No, this creates a variable, named 'a_func', that can hold a pointer
> to a function taking two int arguments and returning an int.


Is it the declaration 'a_func' satisfies the criteria of definition
when 'a_func' pointes with the properly defined function (as you said)? Or I am missing some simple thing?


>No 'extern' to see seen in front of it, so it's a definition, not a
> declaration.


I dont understand.


--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com




Keith Thompson 05-28-2005 03:39 AM

Re: how to define a function pointer variable witout typdef?
 
"sathyashrayan" <sathyashrayan@REMOVETHISgmail.com> writes:
> <Jens.Toerring@physik.fu-berlin.de> wrote in message
> news:3fp26aF8nda3U1@uni-berlin.de...
>> sathyashrayan <sathyashrayan@removethisgmail.com> wrote:
>>
>> > "baumann@pan" <baumann.Pan@gmail.com> wrote in message
>> > news:1117158824.201053.225240@o13g2000cwo.googlegr oups.com...
>> >> typedef int (*pfunc)(int , int);
>> >> pfunc a_func;
>> >>
>> >> i know it's ok,
>> >>
>> >> but how can define a_func without typedef statement?

>>
>> > int (*a_func)(int, int);
>> > But this is a declaration not a definition.

>>
>> No, this creates a variable, named 'a_func', that can hold a pointer
>> to a function taking two int arguments and returning an int.

>
> Is it the declaration 'a_func' satisfies the criteria of definition
> when 'a_func' pointes with the properly defined function (as you
> said)? Or I am missing some simple thing?


It's an object definition (it defines the pointer-to-function object
"a_func"). It's just not a function definition.

Roughly speaking, a definition is a declaration that creates the
entity being declared, whereas a declaration that isn't a definition
merely declares that the entity exists, but doesn't actually create
it. (All definitions are declarations.) For example:

int x; /* a definition; it creates x */
extern int y; /* not a definition; y is defined elsewhere */
void foo(void) { printf("Hello\n"); }
/* a definition of the function "foo" */
void bar(void); /* not a definition; bar is defined elsewhere */

Typedefs are a bit odd in that a typedef doesn't actually create a new
type, merely an alias for an existing type. But a typedef is a
definition because the thing it creates is the alias, not the type.

And now we wait for the experts to point out my errors.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


All times are GMT. The time now is 11:42 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.