473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Problems with casting pointers

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
5 1314
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
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


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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new...
8
by: rihad | last post by:
Hi, I've this question: suppose we have two differently typed pointers: struct foo *foo; char **array; Becase one can always legally cast any pointer to (void *), and becase (void *) is...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
9
by: bwaichu | last post by:
I am starting this thread after having run into two separate programming problems, where the compiler offered no help. The compiler did not even warn. The programs compiled fine. And the...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
9
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.