Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointers to function problem

Reply
Thread Tools

pointers to function problem

 
 
Mantorok Redgormor
Guest
Posts: n/a
 
      04-04-2004
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.



--
nethlek
 
Reply With Quote
 
 
 
 
Christian Bau
Guest
Posts: n/a
 
      04-05-2004
In article <(E-Mail Removed) >,
http://www.velocityreviews.com/forums/(E-Mail Removed) (Mantorok Redgormor) wrote:

> 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.
 
Reply With Quote
 
 
 
 
Jeremy Yallop
Guest
Posts: n/a
 
      04-05-2004
Mantorok Redgormor wrote:
> 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.
 
Reply With Quote
 
Mantorok Redgormor
Guest
Posts: n/a
 
      04-05-2004
Christian Bau <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> In article <(E-Mail Removed) >,
> (E-Mail Removed) (Mantorok Redgormor) wrote:
>
> > 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.


what section?



--
nethlek
 
Reply With Quote
 
Leor Zolman
Guest
Posts: n/a
 
      04-05-2004
On 5 Apr 2004 06:18:31 -0700, (E-Mail Removed) (Mantorok Redgormor) wrote:


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

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

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Smart pointers and member function pointers n2xssvv g02gfr12930 C++ 3 11-27-2005 10:51 AM
void pointers & void function pointers Peter Goddard C Programming 3 05-16-2005 09:44 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Template specialization of pointers with function pointers Phil C++ 1 09-16-2003 02:17 AM



Advertisments