473,703 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function pointer casting

hello all.

i am trying to get rid of some warnings and do "the right thing".
although in this particular case, i am not sure what the right thing
is.

the code:

typedef struct
{
void *ctx;
void (*init)(void *);
void (*update)(void *, const void *, unsigned long);
void (*final)(unsign ed char *, void *);
int mdlen;
char *name;
} digest_t;

MD2_CTX md2ctx;
MD4_CTX md4ctx;
MD5_CTX md5ctx;
SHA_CTX shactx;
RIPEMD160_CTX rmdctx;

digest_t dig[] =
{
{ &md2ctx, MD2_Init, MD2_Update, MD2_Final,
MD2_DIGEST_LENG TH, "MD2" },
{ &md4ctx, MD4_Init, MD4_Update, MD4_Final,
MD4_DIGEST_LENG TH, "MD4" },
{ &md5ctx, MD5_Init, MD5_Update, MD5_Final,
MD5_DIGEST_LENG TH, "MD5" },
{ &shactx, SHA_Init, SHA_Update, SHA_Final,
SHA_DIGEST_LENG TH, "SHA" },
{ &shactx, SHA1_Init, SHA1_Update, SHA1_Final,
SHA_DIGEST_LENG TH, "SHA1" },
{ &rmdctx, RIPEMD160_Init, RIPEMD160_Updat e, RIPEMD160_Final ,
RIPEMD160_DIGES T_LENGTH, "RIPEMD160" }
};

this code will yield a "warning: initialization from incompatible
pointer type", on every Init, Update, and Final function in dig[].
these functions are all in the format:

void MD5_Init(MD5_CT X *c);
void MD5_Update(MD5_ CTX *c, const void *data, unsigned long len);
void MD5_Final(unsig ned char *md, MD5_CTX *c);

with different types for the context. short of writing a wrapper
function for each of these (or one smart wrapper function for them
all), is there a safe solution to fix these assignments? while i'm at
it, please post other corrections are you see fit to call them.

thanks,
joe
Nov 14 '05 #1
3 2986
On 30 Apr 2004 21:17:25 -0700, jo*******@hotma il.com (joe bruin)
wrote:
hello all.

i am trying to get rid of some warnings and do "the right thing".
although in this particular case, i am not sure what the right thing
is.

the code:

typedef struct
{
void *ctx;
void (*init)(void *);
Here you say the second member of the struct is a function pointer
whose argument is void*.
void (*update)(void *, const void *, unsigned long);
void (*final)(unsign ed char *, void *);
int mdlen;
char *name;
} digest_t;

MD2_CTX md2ctx;
MD4_CTX md4ctx;
MD5_CTX md5ctx;
SHA_CTX shactx;
RIPEMD160_CT X rmdctx;

digest_t dig[] =
{
{ &md2ctx, MD2_Init, MD2_Update, MD2_Final,
MD2_DIGEST_LENG TH, "MD2" },
{ &md4ctx, MD4_Init, MD4_Update, MD4_Final,
MD4_DIGEST_LENG TH, "MD4" },
{ &md5ctx, MD5_Init, MD5_Update, MD5_Final,
MD5_DIGEST_LENG TH, "MD5" },
Here you initialize an instance of the struct and the second member is
initialized to the address of MD5_Init.
{ &shactx, SHA_Init, SHA_Update, SHA_Final,
SHA_DIGEST_LENG TH, "SHA" },
{ &shactx, SHA1_Init, SHA1_Update, SHA1_Final,
SHA_DIGEST_LENG TH, "SHA1" },
{ &rmdctx, RIPEMD160_Init, RIPEMD160_Updat e, RIPEMD160_Final ,
RIPEMD160_DIGES T_LENGTH, "RIPEMD160" }
};

this code will yield a "warning: initialization from incompatible
pointer type", on every Init, Update, and Final function in dig[].
these functions are all in the format:

void MD5_Init(MD5_CT X *c);
But the function MD5_Init actually takes a pointer to MD5_CTX which
apparently is not the same as pointer to void.

When calling a function that expects a void*, you can pass any kind of
object pointer you want because the types are compatible for the
implied assignment of the argument to the parameter. Consider that
the following is legal because the implied conversion is allowed
void *x;
MD5_CTX y;
x = &y;

But a function taking a void* is not the same type as a function
taking a MD5_CTX*. More importantly, they are not compatible for the
implied assignment. Consider that the following is not legal because
the implied conversion is not allowed (you could cast but that is a
different story)
void (*func)(void*);
void MD5_Init(MD5_CT X *c);
func = MD5_INIT;

If you can't do it with an assignment, you can't do it with
initialization (excluding the obvious exception of initializing a char
array with a string which cannot be done with assignment).
void MD5_Update(MD5_ CTX *c, const void *data, unsigned long len);
void MD5_Final(unsig ned char *md, MD5_CTX *c);

with different types for the context. short of writing a wrapper
function for each of these (or one smart wrapper function for them
all), is there a safe solution to fix these assignments? while i'm at
it, please post other corrections are you see fit to call them.

Why not declare and define the functions to match the struct members
(take void* as arguments) and inside each function convert the void*
parameter to the correct pointer type. Something like
void MD5_Init(void *x){
MD5_CTX *c = x;
and the rest of your function body can remain unchanged.
<<Remove the del for email>>
Nov 14 '05 #2
On 30 Apr 2004 21:17:25 -0700, jo*******@hotma il.com (joe bruin)
wrote:
hello all.

i am trying to get rid of some warnings and do "the right thing".
although in this particular case, i am not sure what the right thing
is.

the code:

typedef struct
{
void *ctx;
void (*init)(void *);
Here you say the second member of the struct is a function pointer
whose argument is void*.
void (*update)(void *, const void *, unsigned long);
void (*final)(unsign ed char *, void *);
int mdlen;
char *name;
} digest_t;

MD2_CTX md2ctx;
MD4_CTX md4ctx;
MD5_CTX md5ctx;
SHA_CTX shactx;
RIPEMD160_CT X rmdctx;

digest_t dig[] =
{
{ &md2ctx, MD2_Init, MD2_Update, MD2_Final,
MD2_DIGEST_LENG TH, "MD2" },
{ &md4ctx, MD4_Init, MD4_Update, MD4_Final,
MD4_DIGEST_LENG TH, "MD4" },
{ &md5ctx, MD5_Init, MD5_Update, MD5_Final,
MD5_DIGEST_LENG TH, "MD5" },
Here you initialize an instance of the struct and the second member is
initialized to the address of MD5_Init.
{ &shactx, SHA_Init, SHA_Update, SHA_Final,
SHA_DIGEST_LENG TH, "SHA" },
{ &shactx, SHA1_Init, SHA1_Update, SHA1_Final,
SHA_DIGEST_LENG TH, "SHA1" },
{ &rmdctx, RIPEMD160_Init, RIPEMD160_Updat e, RIPEMD160_Final ,
RIPEMD160_DIGES T_LENGTH, "RIPEMD160" }
};

this code will yield a "warning: initialization from incompatible
pointer type", on every Init, Update, and Final function in dig[].
these functions are all in the format:

void MD5_Init(MD5_CT X *c);
But the function MD5_Init actually takes a pointer to MD5_CTX which
apparently is not the same as pointer to void.

When calling a function that expects a void*, you can pass any kind of
object pointer you want because the types are compatible for the
implied assignment of the argument to the parameter. Consider that
the following is legal because the implied conversion is allowed
void *x;
MD5_CTX y;
x = &y;

But a function taking a void* is not the same type as a function
taking a MD5_CTX*. More importantly, they are not compatible for the
implied assignment. Consider that the following is not legal because
the implied conversion is not allowed (you could cast but that is a
different story)
void (*func)(void*);
void MD5_Init(MD5_CT X *c);
func = MD5_INIT;

If you can't do it with an assignment, you can't do it with
initialization (excluding the obvious exception of initializing a char
array with a string which cannot be done with assignment).
void MD5_Update(MD5_ CTX *c, const void *data, unsigned long len);
void MD5_Final(unsig ned char *md, MD5_CTX *c);

with different types for the context. short of writing a wrapper
function for each of these (or one smart wrapper function for them
all), is there a safe solution to fix these assignments? while i'm at
it, please post other corrections are you see fit to call them.

Why not declare and define the functions to match the struct members
(take void* as arguments) and inside each function convert the void*
parameter to the correct pointer type. Something like
void MD5_Init(void *x){
MD5_CTX *c = x;
and the rest of your function body can remain unchanged.
<<Remove the del for email>>
Nov 14 '05 #3

In article <c6**********@2 16.39.135.16>, Barry Schwarz <sc******@deloz .net> writes:

Why not declare and define the functions to match the struct members
(take void* as arguments) and inside each function convert the void*
parameter to the correct pointer type. Something like
void MD5_Init(void *x){
MD5_CTX *c = x;
and the rest of your function body can remain unchanged.


There's at least one good reason not to do this: if the functions are
ever called directly, rather than through a digest_t variable,
they've lost type safety on the first parameter. It's easy to
imagine a cut-and-paste mistake that would pass an MD2_CTX* where an
MD4_CTX* was expected, for example.

In this particular case, it may be that the functions are only ever
invoked through the structure members, and so type safety on the
first parameter is moot; the structure has to lie about the type of
the first parameter in order to accomodate all the digest functions.

However, in general I'd consider this a case where I'd prefer to keep
the functions declared strictly and cast them when initializing
members of the array - if I did something like this at all. That's
assuming I knew that the implementation used interchangeable function
pointer representations for void (*)(void *) and void (*)(MD2_CTX *),
and so forth.

Alternatively, define wrappers for each of the three functions for
each of the digest types, which take a void* for the first parameter
and call the properly-declared, "public" version of the function.
That requires defining a bunch of wrappers, but you eliminate the
casts and maintain type safety if the functions are called directly.

Yet another - even safer - alternative is to create a wrapper type
for the various context types. The wrapper is a struct with an enum
identifying the context type and a union of the context types. All
the digest functions are changed to take a pointer to this new
wrapper structure and verify that the context is of the correct type.
That gives you a single function pointer type for digest_t and
run-time type safety, so that calls through digest_t variables are
type-checked.

--
Michael Wojcik mi************@ microfocus.com

The way things were, were the way things were, and they stayed that way
because they had always been that way. -- Jon Osborne
Nov 14 '05 #4

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

Similar topics

3
9785
by: ken | last post by:
I am getting this error from a gcc compile and I was wondering whether this was 100% valid. This seems a little extreme to me the c++ cast operators appear to only work on objects which defeats the purpose of them. I had to remove the C++ cast and change to a C one in order for gcc to accept it, we are using permissive and I am getting rid of it. Is this truly correct? const ::rtl::OUString sFactoryCreationFunc =...
10
2335
by: Dirk Vanhaute | last post by:
I have only small knowledge of c++, but I would like to compile the example in http://support.microsoft.com/kb/q246772/ HOWTO: Retrieve and Set the Default Printer in Windows I included "#include <Windows.h>" at the start, and the following goes wrong : BOOL DPGetDefaultPrinter(LPTSTR pPrinterName, LPDWORD pdwBufferSize) { ....
10
10029
by: Barbrawl McBribe | last post by:
Is is possible to use typedefs to cast function pointers? I think I saw this in the WINGs src; grep for '(hashFunc)'. So far, trying to use a typedef to cast function pointers so that a return value of float is changed to float seems not to work; I belive it was like this: typedef int (*foo) (int); function_ptr_bar = (foo)function_that_returns_float;
8
2085
by: Mantorok Redgormor | last post by:
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
41
10033
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 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...
4
6951
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but I'm defining the functions as void*, and then casting when I use them. This code is going into a general purpose framework, and it would be much nicer if the user didn't need to do any casting. Can someone tell me how to set up those typedefs...
3
3652
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
5
11235
by: WittyGuy | last post by:
How to typecast a "function pointer" to "const void*" type in C++ way? int MyFunction (double money); // Function prototype const void* arg = (const void*)MyFunction; // type casting function pointer to const void* in C-style void(*pFunc)() = (void(*)())(arg); // type casting const void* to function pointer in C-style (*pFunc)(); // Calling the function after type casting is done
20
2228
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to think my way through what I want to do now. I don't know why - I'm fine with most other aspects of the language, but my brain goes numb when I'm reading about function pointers! I would like to have an array of structures, something like
7
3822
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t event); so I declared a function pointer like typedef void (CFObject::*CFEventHandler)(CFEvent *theEvent);
0
8741
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8658
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9239
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9096
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7842
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5919
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4416
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2419
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.