how to define a function pointer variable witout typdef?

B

baumann@pan

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
 
R

Robert Gamble

baumann@pan said:
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
 
M

Martin Ambuhl

baumann@pan said:
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
 
S

sathyashrayan

baumann@pan said:
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
 
J

Jens.Toerring

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
 
C

CBFalconer

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:
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
 
P

pete

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.
 
E

E. Robert Tisdale

baumann@pan said:
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
 
S

sathyashrayan

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
 
K

Keith Thompson

sathyashrayan said:
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.
 
E

Emmanuel Delahaye

E

Emmanuel Delahaye

sathyashrayan wrote on 27/05/05 :
int (*a_func)(int, int);
But this is a declaration not a definition.

It actually is a defintion of a non initialized pointer.

extern int (*a_func)(int, int);

would be a declaration

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
 
E

Emmanuel Delahaye

sathyashrayan wrote on 28/05/05 :
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?

You are making a mixture here.

/* declaration of x */
extern int x;

/* definition of x with an undefined value */
int x;

/* definition of x with a defined value */
int x = 123;

An unitialized variable has a undefined value, but it occupies a region
of memory. The fact that the value is undefined doesn't turn the
variable to be undefined.

Sounds to be a common mistake...
I dont understand.

That's the point! extern is used to declare a public variable. Read
your C-book again.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
 
P

pete

Emmanuel said:
sathyashrayan wrote on 27/05/05 :

It actually is a defintion of a non initialized pointer.

extern int (*a_func)(int, int);

would be a declaration

But the defintion of a non initialized pointer,
is also a declaration.

N869
6.7 Declarations
Semantics
[#5]
A definition of an
identifier is a declaration for that identifier that:
-- for an object, causes storage to be reserved for that
object;
-- for a function, includes the function body;
 
E

Emmanuel Delahaye

pete wrote on 28/05/05 :
Emmanuel said:
sathyashrayan wrote on 27/05/05 :

It actually is a defintion of a non initialized pointer.

extern int (*a_func)(int, int);

would be a declaration

But the defintion of a non initialized pointer,
is also a declaration.

N869
6.7 Declarations
Semantics
[#5]
A definition of an
identifier is a declaration for that identifier that:
-- for an object, causes storage to be reserved for that
object;
-- for a function, includes the function body;

Yes the 'declaration' thing is embedded into the definition.

But a stand-alone declaration certainely is not a defintion.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
J

Joe Wright

Keith Thompson wrote:

[ much snippage ]
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.

I suppose a typedef is not a definition. This because it does not create
an object in memory. It's another case of C overloading the English
language. Odd, isn't it that '#define X 2' is not a definition either.
 
K

Keith Thompson

Joe Wright said:
Keith Thompson wrote: [snip]
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.

I suppose a typedef is not a definition. This because it does not
create an object in memory. It's another case of C overloading the
English language. Odd, isn't it that '#define X 2' is not a definition
either.

No, it doesn't create an object in memory. Neither does a function
definition. A definition, as I understand it, is a declaration that
creates the named entity, rather than merely referring to an entity
that's created elsewhere. The entity doesn't have to be an object.
 
P

pete

Keith said:
Joe Wright said:
Keith Thompson wrote: [snip]
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.

I suppose a typedef is not a definition. This because it does not
create an object in memory. It's another case of C overloading the
English language.
Odd, isn't it that '#define X 2' is not a definition
either.

No, it doesn't create an object in memory. Neither does a function
definition. A definition, as I understand it, is a declaration that
creates the named entity, rather than merely referring to an entity
that's created elsewhere. The entity doesn't have to be an object.

I only know that typedefs and enums are definitions
because it's been pointed out to me that the standard says so.

External typedefs and enums,
which are both declarations and definitions,
are not definitional enough to be considered as
"external definitions".
 
J

Joe Wright

Keith said:
Joe Wright said:
Keith Thompson wrote:
[snip]
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.

I suppose a typedef is not a definition. This because it does not
create an object in memory. It's another case of C overloading the
English language. Odd, isn't it that '#define X 2' is not a definition
either.


No, it doesn't create an object in memory. Neither does a function
definition. A definition, as I understand it, is a declaration that
creates the named entity, rather than merely referring to an entity
that's created elsewhere. The entity doesn't have to be an object.

Nice side step. In what way is a typedef a declaration creating a named
entity?
 
J

Joe Wright

pete said:
Keith said:
Joe Wright said:
Keith Thompson wrote:
[snip]

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.


I suppose a typedef is not a definition. This because it does not
create an object in memory. It's another case of C overloading the
English language.
Odd, isn't it that '#define X 2' is not a definition
either.

No, it doesn't create an object in memory. Neither does a function
definition. A definition, as I understand it, is a declaration that
creates the named entity, rather than merely referring to an entity
that's created elsewhere. The entity doesn't have to be an object.


I only know that typedefs and enums are definitions
because it's been pointed out to me that the standard says so.

External typedefs and enums,
which are both declarations and definitions,
are not definitional enough to be considered as
"external definitions".

Show me. I have N869 and it makes no reference to typedef reserving
memory for anything. Of course, its very name suggests 'type definition'
in the natural language sense of defining 'last name' as 'surname'.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top