int
main()
{
int i;
char c;
void *the_data;
i = 6;
c = 'a';
the_data = &i;
printf("the_data points to the integer value %d\n", *(int*) the_data);
^^^^^^^
the_data = &c;
printf("the_data now points to the character %c\n", *(char*) the_data);
^^^^^^^^
return 0;
}
On ^^^^ why can't them be typecasted with int and char respectively like
this:
(int) the_data
(char) the_data 14 1488
Logan Lee said:
<snip>
On ^^^^ why can't them be typecasted with int and char respectively like
this:
(int) the_data
(char) the_data
It's a pointer. Interpreting it as something else would be like trying to
interpret the Suez Crisis as a small currant bun topped with icing.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Fri, 28 Dec 2007 10:50:27 +0000에, Logan Lee 썼습니다:
int
main()
{
int i;
char c;
void *the_data;
i = 6;
c = 'a';
the_data = &i;
printf("the_data points to the integer value %d\n", *(int*) the_data);
^^^^^^^
the_data = &c;
printf("the_data now points to the character %c\n", *(char*) the_data);
^^^^^^^^
return 0;
}
On ^^^^ why can't them be typecasted with int and char respectively like
this:
(int) the_data
(char) the_data
i know why (int) the_data... are wrong. but how about (int)*the_data?
Logan Lee said:
<snip>
i know why (int) the_data... are wrong. but how about (int)*the_data?
Because you can't dereference a void *, which in turn is because to do so
would be meaningless.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Fri, 28 Dec 2007 11:12:44 +0000에, Richard Heathfield 썼습니다:
Logan Lee said:
<snip>
>i know why (int) the_data... are wrong. but how about (int)*the_data?
Because you can't dereference a void *, which in turn is because to do so
would be meaningless.
Is (int*)the_data meaningful?
"Logan Lee" <lo*********@student.uts.edu.auschrieb im Newsbeitrag
news:47***********************@news.optusnet.com.a u...
Fri, 28 Dec 2007 11:12:44 +0000?, Richard Heathfield ????:
>Logan Lee said:
<snip>
>>i know why (int) the_data... are wrong. but how about (int)*the_data?
Because you can't dereference a void *, which in turn is because to do so would be meaningless.
Is (int*)the_data meaningful?
Depending on context yes. Here you force the (otherwise meaningless) void *
into an int *, which in this case is exactly what you want, as you assigned
it the address of an int before.
Bye, Jojo
Logan Lee said:
<snip>
Is (int*)the_data meaningful?
It is meaningful if and only if the_data points to an int.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Logan Lee wrote:
Fri, 28 Dec 2007 11:12:44 +0000에, Richard Heathfield 썼습니다:
>Logan Lee said:
<snip>
>>i know why (int) the_data... are wrong. but how about (int)*the_data?
Because you can't dereference a void *, which in turn is because to do so would be meaningless.
Is (int*)the_data meaningful?
"the_data" points at a memory location, but does not point at any
particular data type, and therefore cannot be dereferenced. If the_data
happens to be correctly aligned (as it is in your code), then
(int*)the_data converts it into a pointer which points at the same
memory location, but is now pointing at an 'int'. When you dereference
it, the memory at that location is interpreted as an int, and result is
the value of that int.
Richard Heathfield <rj*@see.sig.invalidwrites:
Logan Lee said:
<snip>
>On ^^^^ why can't them be typecasted with int and char respectively like this: (int) the_data (char) the_data
It's a pointer. Interpreting it as something else would be like trying to
interpret the Suez Crisis as a small currant bun topped with icing.
(Missing context: ``void *the_data;'')
A cast does not interpret something as something else, it *converts*
something to something else. (In some cases, the conversion may just
reinterpret the bits; in others, such as an int-to-float conversion,
it re-expresses the value in the new type.)
<OT>C++ has something called a "misinterpret_cast" -- or is it
"reinterpret_cast"? C doesn't.</OT>
In this case, converting a void* expression to int or to char is
perfectly legal. The result is implementation-defined and is not
necessarily meaningful, though.
In effect, the language says you can convert the Suez Crisis to a
small currant bun topped with icing, but it doesn't guarantee that the
result will be at all edible.
--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Keith Thompson said:
<snip>
A cast does not interpret something as something else, it *converts*
something to something else.
You are, of course, quite correct.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
On Dec 28, 2:50*am, Logan Lee <logan.w....@student.uts.edu.auwrote:
int
main()
{
* int i;
* char c;
* void *the_data;
* i = 6;
* c = 'a';
* the_data = &i;
* printf("the_data points to the integer value %d\n", *(int*) the_data);
* * * * * * * * * * * * * * * * * * * * * * * * * * * ^^^^^^^
* the_data = &c;
* printf("the_data now points to the character %c\n", *(char*) the_data);
* * * * * * * * * * * * * * * * * * * * * * * * * * * ^^^^^^^^
* return 0;
}
On ^^^^ why can't them be typecasted with int and char respectively like
this:
(int) the_data
(char) the_data
It sounds like you are looking for a typeless language. C is not a
typeless language. C++ templates can help but not straight C.
Logan Lee wrote:
>
int main() {
int i;
char c;
void *the_data;
i = 6;
c = 'a';
the_data = &i;
printf("the_data points to the integer value %d\n", *(int*) the_data);
the_data = &c;
printf("the_data now points to the character %c\n", *(char*) the_data);
return 0;
}
On why can't them be typecasted with int and char respectively
like this:
(int) the_data
(char) the_data
Works fine, with the appropriate #include:
[1] c:\c\junk>cat junk.c
#include <stdio.h>
int main(void) {
int i;
char c;
void *the_data;
i = 6;
c = 'a';
the_data = &i;
printf("*the_data is integer value %d\n", *(int*)the_data);
the_data = &c;
printf("*the_data now is the char %c\n", *(char*)the_data);
return 0;
}
[1] c:\c\junk>cc junk.c
[1] c:\c\junk>.\a
*the_data is integer value 6
*the_data now is the char a
--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com
Richard Heathfield wrote:
Logan Lee said:
<snip>
>On ^^^^ why can't them be typecasted with int and char respectively like this: (int) the_data (char) the_data
It's a pointer. Interpreting it as something else would be like
trying to interpret the Suez Crisis as a small currant bun topped
with icing.
I'm getting hungry.
--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com st**************@gmail.com writes:
On Dec 28, 2:50*am, Logan Lee <logan.w....@student.uts.edu.auwrote:
>int main() { * int i; * char c; * void *the_data;
* i = 6; * c = 'a';
* the_data = &i; * printf("the_data points to the integer value %d\n", *(int*) the_data); * * * * * * * * * * * * * * * * * * * * * * * * * * * ^^^^^^^ * the_data = &c; * printf("the_data now points to the character %c\n", *(char*) the_data); * * * * * * * * * * * * * * * * * * * * * * * * * * * ^^^^^^^^ * return 0;
}
On ^^^^ why can't them be typecasted with int and char respectively like this: (int) the_data (char) the_data
It sounds like you are looking for a typeless language. C is not a
typeless language. C++ templates can help but not straight C.
What makes you think the OP is looking for a typeless language? I
have no idea *why* he wants to cast the_data to int or to char. (In a
typeless language, casting would be meaningless.)
--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
On Dec 28, 8:38*pm, Keith Thompson <ks...@mib.orgwrote:
steve.pagliar...@gmail.com writes:
On Dec 28, 2:50*am, Logan Lee <logan.w....@student.uts.edu.auwrote:
int
main()
{
* int i;
* char c;
* void *the_data;
* i = 6;
* c = 'a';
* the_data = &i;
* printf("the_data points to the integer value %d\n", *(int*) the_data);
* * * * * * * * * * * * * * * * * ** * * * * * * * * ^^^^^^^
* the_data = &c;
* printf("the_data now points to the character %c\n", *(char*) the_data);
* * * * * * * * * * * * * * * * * ** * * * * * * * * ^^^^^^^^
* return 0;
}
On ^^^^ why can't them be typecasted with int and char respectively like
this:
(int) the_data
(char) the_data
It sounds like you are looking for a typeless language. C is not a
typeless language. C++ templates can help but not straight C.
What makes you think the OP is looking for a typeless language? *I
have no idea *why* he wants to cast the_data to int or to char. *(In a
typeless language, casting would be meaningless.)
--
Keith Thompson (The_Other_Keith) <ks...@mib.org>
[...]
"We must do something. *This is something. *Therefore, we must do this.."
* * -- Antony Jay and Jonathan Lynn, "Yes Minister"- Hide quoted text -
- Show quoted text -
As you say I was focusing on the why and pointing out that C does not
intrinsically handle polymorphic types. In his example he wants to
treat the_data as a polymorphic type. Perhaps I'm reading too much
into his quetion and it is in fact about casts. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Master of C++ |
last post by:
Hello Folks,
I have a programming dilemma with templates and "genericity". I can
think of a few solutions, but I am not sure which one is the best in
terms of genetic C++ style. Any suggestions...
|
by: Dodo Cibelli |
last post by:
Hello
I'd like to write a Generic class that use a pointer to the generic structure.
I can't do this
unsafe public class PClass<S> where S : new()
{
public S* pData;
public PClass( )
|
by: Albert |
last post by:
Hello all, I've got several questions and I'm sure you'll find them
easy to answer, but sadly I don't know the answers though:
What do people mean by generic pointers
Why is a pointer to void...
|
by: redefined.horizons |
last post by:
First, I would thank all of those that took the time to answer my
question about creating an array based on a numeric value stored in a
variable.
I realize after reading the responses and doing...
|
by: palani12kumar |
last post by:
Can any one tell me what is generic poiner?
im struggling a lot to know what it is?
|
by: interX |
last post by:
Hi
I'm new in VC++ and have a question to generics. I have a generic
class, which contains an array of the generic type. This array I can
pin and then I would like to get an unmanaged pointer to...
|
by: software |
last post by:
Hello,
I am a new bee to c++/cli, here's my question: I have a template
version of a smart pointer class I can't use in my application because
I need to use it in more than one assembly. I...
|
by: juerg.lemke |
last post by:
Hi everyone
I am interested in having multiple functions with different prototypes
and deciding, by setting a pointer, which of them to use later in the
program.
Eg:
int f1(void);
char*...
|
by: Nadeem Afroz |
last post by:
Hello Folks!!!!
i have one interesting question over here ..........
Is it possible to create a generic pointer (generic pointer to member functions) which can point to all the member...
|
by: copx |
last post by:
Why doesn't the C standard include generic function pointers?
I use function pointers a lot and the lack of generic ones is not so cool.
There is a common compiler extension (supported by GCC...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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 projectplanning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |