function pointers

R

ramu

Hi,
Can we assign a function pointer to a data pointer( pointer to
any variable)?

Thanks in Advance
 
K

Keith Thompson

There is no guarantee that function pointers and data pointers
have the same number of bits. And even if you do make the assignment,
that doesn't mean you can use it to read the code.

x86 memory models on MS-DOS are a good example here: function and
data pointers may be 16 or 32 bits, and all 4 possible combinations
are possible. And actually implemented.

As always, Gordon deliberately deletes attribution lines. I've
restored credit for ramu's writing in this followup.

There *is* a guarantee that attempting to assign a function pointer
value to a data pointer object is a constraint violation.

An explicit conversion (i.e., a cast) is not a constraint violation,
but the standard does not define the behavior of such a conversion --
which means, of course, that the behavior is undefined.

(Note to Gordon: Permission to quote this article, or anything else I
post to Usenet, without attribution is expressly denied. Permission
to quote with proper attribution is gladly granted.)
 
S

santosh

ramu said:
Hi,
Can we assign a function pointer to a data pointer( pointer to
any variable)?

Not portably, i.e., you can attempt to do so, but the behaviour is
undefined. A function pointer can be converted to any other type of
function pointer. Any data pointer can be converted to a void * and
converted back. Other pointer conversions are implementation defined.

Why do you want to do this?
 
R

rahul

Hi,
     Can we assign a function pointer to a data pointer( pointer to
any variable)?

Thanks in Advance

Doing that without a cast will generate a diagnostic by a standard
complying compiler. As already pointed out, function pointers and
object pointers can have different sizes. Keith has quoted a real time
example.

If you need to have a generic function pointer, you can cast one type
of function pointer to other.

typedef void (*genPointer)(void);

Here genPointer defines a generic function pointer(you can have any
signature; not necesarrily void parameter and return type).

int foo(int ) {}
/* other things */
genPointer aPointer = foo; /* &foo */
 
V

vippstar

If you need to have a generic function pointer, you can cast one type
of function pointer to other.

typedef void (*genPointer)(void);

Here genPointer defines a generic function pointer(you can have any
signature; not necesarrily void parameter and return type).

int foo(int ) {}
/* other things */
genPointer aPointer = foo; /* &foo */

Shouldn't that be genPointer aPointer = (genPointer)foo;?
 

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
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top