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?? 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".
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.
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
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
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"? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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
|
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...
|
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 :
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |