generic pointer question

L

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
 
L

Logan Lee

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?
 
R

Richard Heathfield

Logan Lee said:

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.
 
R

Richard Heathfield

Logan Lee said:

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.
 
L

Logan Lee

Fri, 28 Dec 2007 11:12:44 +0000ì—, Richard Heathfield ì¼ìŠµë‹ˆë‹¤:
Logan Lee said:



Because you can't dereference a void *, which in turn is because to do so
would be meaningless.

Is (int*)the_data meaningful?
 
J

Joachim Schmitz

Logan Lee said:
Fri, 28 Dec 2007 11:12:44 +0000?, Richard Heathfield ????:


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
 
J

James Kuyper

Logan said:
Fri, 28 Dec 2007 11:12:44 +0000ì—, Richard Heathfield ì¼ìŠµë‹ˆë‹¤:


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.
 
K

Keith Thompson

Richard Heathfield said:
Logan Lee said:



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.
 
R

Richard Heathfield

Keith Thompson said:

A cast does not interpret something as something else, it *converts*
something to something else.

You are, of course, quite correct.
 
S

steve.pagliarulo

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.
 
C

CBFalconer

Logan said:
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
 
C

CBFalconer

Richard said:
Logan Lee said:



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.
 
K

Keith Thompson

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.)
 
S

spaglia

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) <[email protected]>
[...]
"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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top