473,385 Members | 1,548 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,385 software developers and data experts.

pointers to function problem

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
Nov 14 '05 #1
8 2055
In article <41**************************@posting.google.com >,
ne*****@tokyo.com (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.
Nov 14 '05 #2
In article <41**************************@posting.google.com >,
ne*****@tokyo.com (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.
Nov 14 '05 #3
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.
Nov 14 '05 #4
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.
Nov 14 '05 #5
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote in message news:<ch*********************************@slb-newsm1.svr.pol.co.uk>...
In article <41**************************@posting.google.com >,
ne*****@tokyo.com (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
Nov 14 '05 #6
Christian Bau <ch***********@cbau.freeserve.co.uk> wrote in message news:<ch*********************************@slb-newsm1.svr.pol.co.uk>...
In article <41**************************@posting.google.com >,
ne*****@tokyo.com (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
Nov 14 '05 #7
On 5 Apr 2004 06:18:31 -0700, ne*****@tokyo.com (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
Nov 14 '05 #8
On 5 Apr 2004 06:18:31 -0700, ne*****@tokyo.com (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
Nov 14 '05 #9

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

Similar topics

26
by: Desmond Liu | last post by:
I've read articles like Scott Meyer's EC++ (Item 22) that advocate the use of references when passing parameters. I understand the reasoning behind using references--you avoid the cost of creating...
2
by: V. de Bellabre | last post by:
Hello all, I'm currently working on a 3D graphic engine but I'm stuck with a small problem on references of pointers. Some of my c++ class are complex and use many pointers so I decided to force...
3
by: Jan-Henrik Grobe | last post by:
Hallo, I am coming to this newsgroup with a very strange Problem. I have two c++ files A.cpp and B.cpp....In A.cpp I am creating an openGL window and in B.cpp I have stored the callback...
53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
8
by: Klaas Vantournhout | last post by:
Hi all, I'm in need of a matrix of function pointers, and to be honest. No 'nice' solution has been found yet on that big big internet. It is possible to declare a matrix of function pointers...
11
by: Michael | last post by:
Hi, I am trying to get an idea of how function pointers work. I have the following: #include <stdio.h> void do_stuff(int*,int,void*); void getInt(int*); void showInt(int*);
7
by: Erdal Mutlu | last post by:
Hi, I am trying to design a base class (interface) with two or more subclasses as follows: class A { .... virtual static A* getByName(const string x)=0 const; }
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
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

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.