Pointer Indirection Syntax

  • Thread starter Thomas Matthews
  • Start date
T

Thomas Matthews

Hi,

At my work, we have a need for a pointer to a pointer
to a function returning void and having void parameters.

Since this is an embedded system, we have to assign the
variable with a constant value (address).

We came up with the following:
#define LOCATION 0x10000
typedef void (*FuncPtr)(void);
typedef *FuncPtr PtrFuncPtr;

/* usage */
if (*((PtrFuncPtr) LOCATION) != NULL)
{
(**LOCATION)(); /* Execute function */
}

The above "if" statement should be translated to:
If the value at 0x10000 is not NULL,
execute the function pointed to by the
value in location 0x10000.
The function takes void parameters and returns void.

What is the syntax for declaring a pointer to a pointer
to a function taking no parameters and returning void?
{In other words, what is the syntax to combine the
two typedefs into one?}
{Or /where does the 2nd asterisk (*) get placed?}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

Joona I Palaste

Thomas Matthews said:
At my work, we have a need for a pointer to a pointer
to a function returning void and having void parameters.
Since this is an embedded system, we have to assign the
variable with a constant value (address).
We came up with the following:
#define LOCATION 0x10000
typedef void (*FuncPtr)(void);
typedef *FuncPtr PtrFuncPtr;
/* usage */
if (*((PtrFuncPtr) LOCATION) != NULL)
{
(**LOCATION)(); /* Execute function */
}
The above "if" statement should be translated to:
If the value at 0x10000 is not NULL,
execute the function pointed to by the
value in location 0x10000.
The function takes void parameters and returns void.
What is the syntax for declaring a pointer to a pointer
to a function taking no parameters and returning void?
{In other words, what is the syntax to combine the
two typedefs into one?}
{Or /where does the 2nd asterisk (*) get placed?}

Let me try this by hand. I might screw up, so any C gurus out there,
feel free to correct me.
Function taking no parameters and returning void:
void function(void)
Pointer to a function taking no parameters and returning void:
void (*function)(void)
Pointer to a pointer to a function taking no parameters and returning
void:
void (**function)(void)
I believe that's it.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates
 
E

Eric Amick

Let me try this by hand. I might screw up, so any C gurus out there,
feel free to correct me.
Function taking no parameters and returning void:
void function(void)
Pointer to a function taking no parameters and returning void:
void (*function)(void)
Pointer to a pointer to a function taking no parameters and returning
void:
void (**function)(void)
I believe that's it.

That's fine, and the original typedefs were *almost* correct. Note the
change in the second:

typedef void (*FuncPtr)(void);
typedef FuncPtr *PtrFuncPtr;

In general, to create a typedef, declare a variable of the appropriate
type, then stick "typedef" in front.
 

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
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top