By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,866 Members | 1,765 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,866 IT Pros & Developers. It's quick & easy.

Problems with casting pointers

P: n/a
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??
Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
JS wrote:
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".
Nov 14 '05 #2

P: n/a
JS wrote:
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.
Nov 14 '05 #3

P: n/a


JS wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #4

P: n/a
JS wrote:

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.

--
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
Nov 14 '05 #5

P: n/a
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"?
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.