Problems with casting pointers

J

JS

I give a function a void pointer as an argument. But in the function I would
like to treat this argument as an integer (only pointers to integers will be
sent to the function) therefor I would like to cast it into an int pointer.


I found the following example:

void use_int(void *r) {
int a;
a = * (int *) r;
printf("As an integer, you are %d years old.\n", a);
}


but I don't get this line:

a = * (int *) r;

does it mean that a is a pointer to a pointer??
 
M

Mark Odell

JS said:
I give a function a void pointer as an argument. But in the function I would
like to treat this argument as an integer (only pointers to integers will be
sent to the function) therefor I would like to cast it into an int pointer.


I found the following example:

void use_int(void *r) {
int a;
a = * (int *) r;
printf("As an integer, you are %d years old.\n", a);
}


but I don't get this line:

a = * (int *) r;

does it mean that a is a pointer to a pointer??

No, it means "treat r as a pointer-to-int and then dereference it to get
that int's value".
 
E

Eric Sosman

JS said:
I give a function a void pointer as an argument. But in the function I would
like to treat this argument as an integer (only pointers to integers will be
sent to the function) therefor I would like to cast it into an int pointer.


I found the following example:

void use_int(void *r) {
int a;
a = * (int *) r;
printf("As an integer, you are %d years old.\n", a);
}


but I don't get this line:

a = * (int *) r;

does it mean that a is a pointer to a pointer??

`a' is an int, just as the `int a;' declaration says
it is. Read the puzzling expression this way:

a = *( (int*) r );

`r' is a `void*', a pointer to void. `(int*) r' is that
pointer's value converted to an `int*', a pointer to int.
`*( (int*) r )' is `*( pointer_to_int )', that is, the int
value that is pointed to.
 
M

Martin Ambuhl

JS said:
I give a function a void pointer as an argument. But in the function I would
like to treat this argument as an integer (only pointers to integers will be
sent to the function) therefor I would like to cast it into an int pointer.


I found the following example:

void use_int(void *r) {
int a;
a = * (int *) r;
printf("As an integer, you are %d years old.\n", a);
}
but I don't get this line:
a = * (int *) r;
does it mean that a is a pointer to a pointer??

r, which is a pointer to void, converted a pointer to int
(int *)r
that pointer is dereferenced
*(int *)r
and the result, an int, is assigned to a
a = *(int *)r;

Note that r might not point to a region of memory that can be
interpreted as an int, because of alignment issues, for example, and so
this code should be used with caution.
 
C

CBFalconer

JS said:
I give a function a void pointer as an argument. But in the
function I would like to treat this argument as an integer (only
pointers to integers will be sent to the function) therefor I
would like to cast it into an int pointer.

The proper cast free mechanism example, which depends on the fact
that the void* will always be supplied pointing to storage for an
int, is:

T foo(void *bar)
{
int *p = bar;

/* code using p and *p, but not bar */
return /* something of T type */;
}

The compiler may well optimize away the existance of p. This
follows the dictum that all casts are suspicious, and should be
avoided wherever possible.
 
P

Peter Shaggy Haywood

Groovy hepcat JS was jivin' on Tue, 22 Mar 2005 21:09:32 +0100 in
comp.lang.c.
Problems with casting pointers's a cool scene! Dig it!
I give a function a void pointer as an argument. But in the function I would
like to treat this argument as an integer (only pointers to integers will be
sent to the function)

Then why not make the parameter a pointer to int? That would make
more sense and remove the need for a cast.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

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
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top