Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > generic pointer question

Reply
Thread Tools

generic pointer question

 
 
Logan Lee
Guest
Posts: n/a
 
      12-28-2007
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
 
Reply With Quote
 
 
 
 
Logan Lee
Guest
Posts: n/a
 
      12-28-2007
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?
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      12-28-2007
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
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-28-2007
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
 
Reply With Quote
 
Logan Lee
Guest
Posts: n/a
 
      12-28-2007
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?
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      12-28-2007
"Logan Lee" <(E-Mail Removed)> schrieb im Newsbeitrag
news:4774dc03$0$13262$(E-Mail Removed) 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


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-28-2007
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
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-28-2007
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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-28-2007
Richard Heathfield <(E-Mail Removed)> writes:
> 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) <(E-Mail Removed)>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-28-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
not just generic type programming,but also parallism generic syntaxprogramming?? minlearn C++ 2 03-13-2009 05:17 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments