char* vs void*

Lars Wirzenius wirzeniu at cs.Helsinki.FI
Sat Dec 29 05:19:41 AEST 1990


In article <17590 at paperboy.OSF.ORG> dbrooks at osf.org (David Brooks) writes:
>In article <2778A795.6E71 at tct.uucp> chip at tct.uucp (Chip Salzenberg) writes:
>>
>>The ANSI C standard requires that |char *| and |void *| have identical
>>representations.  This requirement bows to existing practice.  After
>> [deleted]
>
>How's that again, again?
>
>I don't understand.  In the case of void*, what is being represented?

Pointers of type |char *| and |void *| are capable of pointing to *any*
data object and that the way the compiler stores those types of pointers
(their representation, that is) is the same.  Given two pointer
variables,

	void *void_ptr;
	char *char_ptr;

and some data object, say data_object, and you assign the address of
that object to both pointers, their bit patterns will be exactly the
same: 

	void_ptr = &data_object;
	char_ptr = (char *) &data_object;
	printf("%d", memcmp(&void_ptr, &char_ptr, sizeof(void *)));

(the sizes of the two pointers are the same).

The difference between |char *| and |void *| is that you can mix 
|void *| with pointers to other types without having to use casts. With
|char *|, and any other pointer type except |void *|, you need the casts
to make sure everything works on all machines.

Lars Wirzenius    wirzeniu at cs.helsinki.fi    wirzenius at cc.helsinki.fi



More information about the Comp.lang.c mailing list