how to define a function pointer variable witout typdef?

T

Tim Rentsch

My own rule of thumb is to never create a typedef name for an object
pointer type, unless that type is meant to be opaque and never
dereferenced directly.

I'd like to offer another case for consideration.

It makes sense to use typedef to define a type name for an object
pointer type when the pointed-to type is supposed always to be
allocated in the heap and never as a regular (either static or auto)
variable. This rule can work especially well with struct's:

typedef struct {
/* ... yada, yada, yada... */
} *Yada;

Since there is no name for the 'struct' type itself, it can never be
used accidentally to declare a local variable that's an instance of
the struct; this can avoid certain kinds of programming errors.
 
P

pete

Keith Thompson wrote:
More seriously, I'll stand by my previous discussions about what a
"definition" *should* be, but I must acknowledge that the standard
doesn't use the term that way.

This is what sets object definitions and function definitions
apart from all other declarations in a translation unit:
They get translated all the way, to become program instructions.
All other declarations and defintions
are just information about types for the compiler.
 
P

pete

Joe Wright wrote:
The point of using 'pointer' as the object and 'address'
as the value is
to make my explanations more intelligible. Using the terms
interchangeably fosters confusion. The Standard gets this wrong.

There's times when the distinction isn't important
and then it's cumbersome to say "pointers and addresses".

N869
7.21 String handling <string.h>
7.21.1 String function conventions
[#2]
"pointer arguments on such a call shall still
have valid values"
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top