473,378 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

typedef function pointer

Hi (first, excuse me for my bad english)

As I know, the semantics for typedef is:

typedef A B;

I think this makes B a synonym of A, where A is an existing data type. Is
that right?

Based on the previous definition of typedef, I can't understand the next:

typedef int (*TypeFunc) (int, int);

I think it's declaring a new type called TypeFunc. The objects of that
type are functions returning int and accepting two integer parameters.
But I can't understand the semantic of the last expresion. Based on the
first definition, the last should be:

typedef int (*) (int, int) TypeFunc;
/* first the existing datatype (pointer to function) and then the new
identifier datatype. */

Well, as you can see, I really can't understand the logic of the
semantics of typedef....

Thanks

--
Sebastián Gurin (Cancerbero)
cancerbero_sgx AT users.sourceforge.net

Nov 14 '05 #1
5 41006
Cancerbero <sg****@montevideo.com.uy> wrote:
As I know, the semantics for typedef is: typedef A B; I think this makes B a synonym of A, where A is an existing data type. Is
that right?
Only in the most simple cases, so not really;-)
Based on the previous definition of typedef, I can't understand the next: typedef int (*TypeFunc) (int, int); I think it's declaring a new type called TypeFunc. The objects of that
type are functions returning int and accepting two integer parameters.
That's correct if you replace 'functions' by 'pointers to functions'.
And it defines just a new identifier for a type, not a new type (because
'pointer to function returning int and taking two int arguments' isn't
a new type, all you do is creating a new name for it).
But I can't understand the semantic of the last expresion. Based on the
first definition, the last should be: typedef int (*) (int, int) TypeFunc;
/* first the existing datatype (pointer to function) and then the new
identifier datatype. */


But your first definition was too restrictive. It is as if you were
saying that a definition of a variable 'v' of type A always has the
form

A v;

But while that holds in some cases, it doesn't cover cases like a
definition of a function pointer, which you have to define like this

int ( * func_ptr ) ( int, int );

making 'func_ptr' a pointer to a function returning int and taking
two int arguments.

Now, if you compare that to the typedef you are worried about

typedef int ( * TypeFunc ) ( int, int );

you immediately see that a better description of the syntax of typedef
is that the new name you want to typedef goes in the place where in a
variable definition with that type the name of the variable would appear.

Another example would be a typedef for an array type, e.g. an array
of 10 ints. You would define such an array 'x' as

int x[ 10 ];

so, consequently, the typedef for an array of 10 ints must look
like this:

typedef int arr_of_ten_ints[ 10 ];

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
Cancerbero <sg****@montevideo.com.uy> writes:
Hi (first, excuse me for my bad english)

As I know, the semantics for typedef is:

typedef A B;

I think this makes B a synonym of A, where A is an existing data type. Is
that right?
That's the syntax, not the semantics. "Syntax" refers to the way a
construct is written in C source; "semantics" refers to what it means.
(And actually that's not the syntax of a typedef, except in the
simplest cases.)

As Jens points out, it's like saying that the syntax of an object
declaration is
A v;
where v is the object name and A is the type. But if you wnat v
to be an array of 20 ints, the declaration is
int v[20];
which doesn't fit the simple "A v;" syntax.

But yes, "typedef A B;" makes B a synonym of A. That's the semantics.
Based on the previous definition of typedef, I can't understand the next:

typedef int (*TypeFunc) (int, int);


Syntactically, "typedef" is treated as a storage-class specifier, like
"extern", "static", "auto", or "register". It really isn't one (it
doesn't specify any kind of storage class), but it turned out to be
convenient to define the syntax that way (though one could argue that
it's not really all that convenient after all). So if you can
understand that

extern int (*Typefunc) (int, int);

declares an external pointer object called "Typefunc", that points
to a function taking to int arguments and returning it, then you can
understand that

typedef int (*Typefunc) (int, int);

declares a type name called "Typefunc", that refers to a pointer to a
function taking to int arguments and returning it.

Now it's just a matter of understanding C's declaration syntax.
Unfortunately, that's a non-trivial task.

The basic principle is that the declaration of an entity mirrors the
use of that entity. For example:
int *a;
declares a as a pointer to int. You can also think of it as declaring
that "*a" is an int.

Dropping the extern/typedef and changing the name, consider:
int (*foo) (int, int);
Back off a bit and look at the declaration as a whole. It says that
(*foo) (int, int)
will yield an int value. It looks like a function call, so the stuff
before the arguments
(int, int)
has to denote a function, so
(*foo)
denotes a function, so
foo
has to be a pointer to a function. We've already seen that the
function takes two int arguments, and that it returns int, so we can
conclude that
int (*foo) (int, int);
declares foo as a pointer object, specifically as a pointer to a
function taking two int arguments and returning int.

Add "extern", and it declares foo as an extern object of the same type.

Add "typedef" instead of "extern", and it declares foo as a type name
for the same type.

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 14 '05 #3
Cancerbero <sg****@montevideo.com.uy> wrote in message news:<pa*********************************@montevid eo.com.uy>...
Hi (first, excuse me for my bad english)

As I know, the semantics for typedef is:

typedef A B;

I think this makes B a synonym of A, where A is an existing data type. Is
that right?

Almost, but not quite. It works when talking about simple types and
identifiers, but breaks down for cases like what you describe below.

You need to read up on the concept of *declarators*, since they're
unique to C (and C++, and any other language directly derived from C).
The declarator introduces the name being declared and supplies
additional type information. For example, given the declaration

int *a[10];

the storage class is 'auto' (default), the type specifier is 'int',
the declarator is '*a[10]', and the identifier is 'a'. The type of
'a' is "10-element array of pointer to int"; the int-ness of 'a' is
specified through the type specifier, but the pointer-ness and
array-ness are specified through the declarator (incidentally, this is
why statements like "char* a, b;" don't work the way people think they
should; the '*' is associated with the identifier 'a', *not* the type
specifier 'char').

This is because C was designed with a "declaration mimics use"
paradigm. When you reference an item in an array, you use a subscript
operator []. When you dereference a pointer, you use the dereference
operator *. IOW, to get the int value pointed to by the 3rd array
element, you use the expression

*a[2];

which evaluates to an int type. So the idea was to make the
declaration of 'a' look the same as a reference to 'a' in the code. I
know this seemed like a good idea at the time, but in truth it makes
some operations harder than they need to be, and some declarations can
get obnoxiously difficult to write (which is where typedefs come in
handy). I've been programming in C for almost 15 years, and I didn't
*really* understand the concept until fairly recently.

What the typedef storage class does is make the *identifier* within
the declarator a synonym for an existing type. Going with the example
above, 'a' is a 10-element array of pointer to int. So the statement

typedef int *a[10];

creates a synonym for the existing type "10-element array of pointer
to int" and associates it with the identifier 'a'.
Based on the previous definition of typedef, I can't understand the next:

typedef int (*TypeFunc) (int, int);

I think it's declaring a new type called TypeFunc. The objects of that
type are functions returning int and accepting two integer parameters.
But I can't understand the semantic of the last expresion. Based on the
first definition, the last should be:

typedef int (*) (int, int) TypeFunc;
/* first the existing datatype (pointer to function) and then the new
identifier datatype. */

Reference what I said above. The *declarator* is (*TypeFunc) (int,
int), which says that the *identifier* TypeFunc is a pointer to a
function taking two int parameters. TypeFunc becomes the typedef
name.
Well, as you can see, I really can't understand the logic of the
semantics of typedef....

Thanks

Nov 14 '05 #4
Cancerbero wrote:
Based on the previous definition of typedef, I can't understand the next:

typedef int (*TypeFunc) (int, int);


This is one of the essential shortcomings of C - its declaration syntax,
especially for functions, mixes a variety of elements in a confusing
order. The goal, to make function type declarations resemble function
invocations, is for many people a confusion rather than an aid. You can
do much much worse, when you start talking about functions taking or
returning function pointers, or start worrying about where the * or &
goes when qualifiers abound.

In SPECS, an alternate C++ syntax designed by Damian Conway, your
typedef would look like this:

type TypeFunc : ^((int, int) -> int);

The type operator ^ means "pointer to", while X -> Y means "a function
taking X as an argument and returning Y." This is quite a bit closer to
what you suggested, and keeps the type info separate from the new
identifier. See Conway's paper for more:

http://www.csse.monash.edu.au/~damia...tProposal.html

--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #5
Derrick Coetzee <dc****@moonflare.com> scribbled the following:
Cancerbero wrote:
Based on the previous definition of typedef, I can't understand the next:

typedef int (*TypeFunc) (int, int);
This is one of the essential shortcomings of C - its declaration syntax,
especially for functions, mixes a variety of elements in a confusing
order. The goal, to make function type declarations resemble function
invocations, is for many people a confusion rather than an aid. You can
do much much worse, when you start talking about functions taking or
returning function pointers, or start worrying about where the * or &
goes when qualifiers abound. In SPECS, an alternate C++ syntax designed by Damian Conway, your
typedef would look like this: type TypeFunc : ^((int, int) -> int); The type operator ^ means "pointer to", while X -> Y means "a function
taking X as an argument and returning Y." This is quite a bit closer to
what you suggested, and keeps the type info separate from the new
identifier. See Conway's paper for more: http://www.csse.monash.edu.au/~damia...tProposal.html


That looks strangely like Haskell syntax. Perhaps later we will be
seeing typedefs like this:

type TypeFunc: ^(((int, int) -> int) -> (int -> int));

meaning "pointer to a function, which takes a function taking an
(int, int) and returning int, an returns a function taking an int
and returning int".
This is not currently possible in either C or C++ because they can't
manufacture new functions at run-time but it would be nice in some
new mixed-paradigm language.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'I' is the most beautiful word in the world."
- John Nordberg
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: LinusLee | last post by:
class CAnalyzer { // attribute private: public: // behavior private: void NullFunc(NODE *pNode); void TraverseNode(NODE* pNode, void (*preFunc)(NODE *node), void (*postFunc)(NODE *node));
8
by: Bryan Parkoff | last post by:
I have written over 4096 functions for my project. I would like to create one Function Pointer so Function Pointer will contain 4096 functions. I do not want Function Pointer to be stored in the...
3
by: tomailmelookatbottom | last post by:
I am a beginner in C++. I recently downloaded the codes of C++ faq book.Since I don't have the book ,following is two program which is confusing me a lot. program 38_01.cpp ^^^^^^^^^^^^^^^^^...
4
by: ranjmis | last post by:
Hi all, I have come across a piece of code wherein a function returns a function pointer as it seems to me but not very clear from the prototype. As shown below - although return type is void...
3
by: robin liu | last post by:
What's the difference between these two declarations ? 1) typedef void (*pf)(void); 2) typedef void f(void); the first declaration is define a function pointer, what is the second ? define a...
4
by: Jeffrey Spoon | last post by:
Hello, I am trying to make a simple function that returns a pointer to another function. In my header file I've declared my function pointer: void (*pStateFunction) (void); //assume the function...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
12
by: viza | last post by:
Hi I have program1.c: typedef int (*fn_t)(int); int fn( int f ){ return f; }
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.