pointers to function problem

  • Thread starter Mantorok Redgormor
  • Start date
M

Mantorok Redgormor

I have ran into a problem where I have a struct
that has a member which contains a pointer
to function and is initialized to a function
in the initializer list. With my array of
structs of this type, I have some elements
of this array, thats function pointer member does
not need to be initialized to a function.
I can't simply initialize it to NULL
and I'm not sure if casting NULL(which can
be 0 or (void *)0) to the function pointer type
will work.

If NULL is (void *)0 on some implementations
you would end up with:

(int (*)(unsigned int))(void *)0

is something like that even guaranteed
to work?

I'm not even sure if just plain 0 and casting
it to the function pointer type would work.
Since function pointers might have different
representations than object pointers, it seems
you would end up with implementation-defined
behavior either way.

Has anyone ran into this problem before?
Where portability was a concern to you
and you couldn't rely on implementation-
defined behavior.
 
C

Christian Bau

I have ran into a problem where I have a struct
that has a member which contains a pointer
to function and is initialized to a function
in the initializer list. With my array of
structs of this type, I have some elements
of this array, thats function pointer member does
not need to be initialized to a function.
I can't simply initialize it to NULL
and I'm not sure if casting NULL(which can
be 0 or (void *)0) to the function pointer type
will work.

If NULL is (void *)0 on some implementations
you would end up with:

(int (*)(unsigned int))(void *)0

is something like that even guaranteed
to work?

NULL is a null pointer constant. Any null pointer constant can be cast
to any pointer type and the result will be a null pointer of the
corresponding type. That is true both for data pointers and function
pointers. It is guaranteed to work.
I'm not even sure if just plain 0 and casting
it to the function pointer type would work.

A plain zero is also a null pointer constant and can be cast to a
function pointer, producing a null pointer of the right type.
Since function pointers might have different
representations than object pointers, it seems
you would end up with implementation-defined
behavior either way.

No, it is defined by the C Standard.
 
J

Jeremy Yallop

Mantorok said:
I have ran into a problem where I have a struct that has a member
which contains a pointer to function and is initialized to a
function in the initializer list. With my array of structs of this
type, I have some elements of this array, thats function pointer
member does not need to be initialized to a function. I can't
simply initialize it to NULL and I'm not sure if casting NULL(which
can be 0 or (void *)0) to the function pointer type will work.

Just initialize it to an uncast null pointer constant (e.g. NULL or
0).

#include <math.h>
#include <stddef.h>

struct {
double (*one)(double),
(*two)(double);
} functions[] = { {sin, NULL}, {NULL, cos} };

Null pointer constants are special in C: assigning a null pointer
constant to a function pointer type is allowed, and has the desired
behaviour.
If NULL is (void *)0 on some implementations
you would end up with:

(int (*)(unsigned int))(void *)0

is something like that even guaranteed
to work?

It's guaranteed to work
I'm not even sure if just plain 0 and casting
it to the function pointer type would work.

There's no need to cast at all.
Since function pointers might have different
representations than object pointers, it seems
you would end up with implementation-defined
behavior either way.

Casts deal with values, not representations.

Jeremy.
 
M

Mantorok Redgormor

Christian Bau said:
NULL is a null pointer constant. Any null pointer constant can be cast
to any pointer type and the result will be a null pointer of the
corresponding type. That is true both for data pointers and function
pointers. It is guaranteed to work.


A plain zero is also a null pointer constant and can be cast to a
function pointer, producing a null pointer of the right type.


No, it is defined by the C Standard.

what section?
 
L

Leor Zolman

what section?

6.3.2.3/3 and 4:

3 An integer constant expression with the value 0, or such an expression
cast to type void *, is called a null pointer constant.55) If a null
pointer constant is converted to a pointer type, the resulting pointer,
called a null pointer, is guaranteed to compare unequal to a pointer to any
object or function.

4 Conversion of a null pointer to another pointer type yields a null
pointer of that type. Any two null pointers shall compare equal.

-leor
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top