开发者

What effect does casting a variable as (void *) have?

In a C program I see the following statement:

memcpy开发者_如何学JAVApgm2ram((void*)&AppConfig.MyMACAddr, (ROM void*)SerializedMACAddress, sizeof(AppConfig.MyMACAddr));

What does the (void*) case do? This is written for the Microchip C30 compiler.

AppConfig is defined like this:

APP_CONFIG AppConfig;  // APP_CONFIG is obviously a structure...

SerializedMACAddress is defined like this:

static ROM BYTE SerializedMACAddress[6] = {MY_DEFAULT_MAC_BYTE1, MY_DEFAULT_MAC_BYTE2, MY_DEFAULT_MAC_BYTE3, MY_DEFAULT_MAC_BYTE4, MY_DEFAULT_MAC_BYTE5, MY_DEFAULT_MAC_BYTE6};

EDIT: I should have stated this before but memcpypgm2ram is defined as: #define memcpypgm2ram(a,b,c) memcpy(a,b,c)

so basically, void *memcpy(void *dest, const void *src, size_t n);


void* is the universal data pointer type, that, when used as an argument type, denotes that a function works on "bare" memory blocks. It cannot be dereferenced.

Any other data pointer type can be implicitly converted to void*, so the explicit cast is probably either wrong (unnecessary), or a workaround for a broken compiler, or a shorthand to cast to unsigned char * (in which case it's a workaround for a broken interface).


The cast void * converts a pointer of some type into a Generic Pointer.


The memcpypgm2ram function does not need a specific type. It should be defined with the following prototype : memcpypgm2ram(void* p1, void* p2, int n); "void *" is a cast to a generic pointer type.

The function just takes two pointers of any type and copy n bytes (in your case n=sizeof(AppConfig.MyMACAddr)) from one address to the other.


memcpypgm2ram presumably takes a void pointer as it's argument. C is strongly enough typed to recognize that the type of &AppConfig.MyMACAddr is (MACAddr*) and will emit a compile-time error if you don't cast it as a void*.

The point is that memcpypgm2ram is a function that works on any bytes held in memory, so it won't accept strongly typed pointers as arguments.


in this case, casting to (void *) may be used to get rid of compiler's warning.


There's no way to say what was the point of using (void *) cast in this specific case without seeing more context (how is memcpypgm2ram declared?).

In C language (as well as in C++) pointer types are implicitly convertible to void * type, which means that in pointer conversions there's usually no reason to use an explicit cast to void * type. It looks like in your example all conversions are pointer conversions, so, taking into account what I said, the explicit cast to void * is not required.

Another possibility though is that the original pointer types are const-qualified, so the cast to void * was used to remove the const-qualification. However, I don't see any const-qualifications in what you provided, which means, again, that most likely that cast to void * is unnecessary. My guess would be that whoever put it there did it "just in case" for no real reason.

In short, the (void *) cast converts the pointers to void * type. But since that conversion would happen implicitly anyway, the cast is totally unnecessary (assuming memcpypgm2ram is declared with void * parameters).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜