:: Re: [DNG] Making sense of C pointer…
Kezdőlap
Delete this message
Reply to this message
Szerző: KatolaZ
Dátum:  
Címzett: Edward Bartolo
CC: dng
Tárgy: Re: [DNG] Making sense of C pointer syntax.
On Wed, Mar 30, 2016 at 09:26:32AM +0200, Edward Bartolo wrote:
> Hi,
>
> I can also understand that typecasting, that is, bypassing strict type
> checking, a void pointer without checking for architecture is like a
> time bomb. Addresses on different architectures can be different sizes
> and a typecast of void from an integer requires some conditions that
> must be true. Both void* and integer must have the same size on the
> target machine or code is written in a way to make sure a difference
> in architecture does not result in code being broken.
>


void* and int happen to have the same size in i386 architectures, but
this is not always true. There is no implied correspondence between
void* and int that I am aware of. In my amd64 machine, all pointers
are 8 bytes, but an int is 4 bytes long.

Please do not make assumptions about the length of a pointer. The
typecasting referred before was *between pointers*, and not between
pointers and other types. This means that you can cast a void* to a
char* or to an int**, but not that you can cast a void* to a char.

Also, pointer arithmetic is defined (to the best of my knowledge) only
on typed pointers, so that something like:

void *a;
a = a +1;

is not meaningful (and I bet you should get a compile-time error),
while:

void *a;
a = (int*) a + 1;

is perfectly valid, and widely used.

My2Cents

KatolaZ

--
[ Enzo Nicosia aka KatolaZ --- GLUG Catania -- Freaknet Medialab ]
[ me [at] katolaz.homeunix.net -- http://katolaz.homeunix.net -- ]
[ GNU/Linux User:#325780/ICQ UIN: #258332181/GPG key ID 0B5F062F ]
[ Fingerprint: 8E59 D6AA 445E FDB4 A153 3D5A 5F20 B3AE 0B5F 062F ]