generic pointer question

Discussion in 'C Programming' started by Logan Lee, Dec 28, 2007.

  1. Logan Lee

    Logan Lee Guest

    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
     
    Logan Lee, Dec 28, 2007
    #1
    1. Advertising

  2. Logan Lee

    Logan Lee Guest

    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, Dec 28, 2007
    #2
    1. Advertising

  3. 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
     
    Richard Heathfield, Dec 28, 2007
    #3
  4. 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
     
    Richard Heathfield, Dec 28, 2007
    #4
  5. Logan Lee

    Logan Lee Guest

    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, Dec 28, 2007
    #5
  6. "Logan Lee" <> schrieb im Newsbeitrag
    news:4774dc03$0$13262$...
    > 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
     
    Joachim Schmitz, Dec 28, 2007
    #6
  7. 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
     
    Richard Heathfield, Dec 28, 2007
    #7
  8. Logan Lee

    James Kuyper Guest

    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.
     
    James Kuyper, Dec 28, 2007
    #8
  9. Richard Heathfield <> 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) <>
    [...]
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 28, 2007
    #9
  10. 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
     
    Richard Heathfield, Dec 28, 2007
    #10
  11. Logan Lee

    Guest

    On Dec 28, 2:50 am, 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


    It sounds like you are looking for a typeless language. C is not a
    typeless language. C++ templates can help but not straight C.
     
    , Dec 28, 2007
    #11
  12. Logan Lee

    CBFalconer Guest

    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
     
    CBFalconer, Dec 28, 2007
    #12
  13. Logan Lee

    CBFalconer Guest

    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
     
    CBFalconer, Dec 28, 2007
    #13
  14. writes:
    > On Dec 28, 2:50 am, 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

    >
    > 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) <>
    [...]
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 29, 2007
    #14
  15. Logan Lee

    spaglia Guest

    On Dec 28, 8:38 pm, Keith Thompson <> wrote:
    > writes:
    > > On Dec 28, 2:50 am, 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

    >
    > > 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) <>
    > [...]
    > "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.
     
    spaglia, Dec 29, 2007
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murat Tasan
    Replies:
    1
    Views:
    8,076
    Chaitanya
    Feb 3, 2009
  2. Replies:
    2
    Views:
    448
  3. Replies:
    10
    Views:
    717
    Chris Torek
    Feb 4, 2005
  4. jimjim
    Replies:
    16
    Views:
    852
    Jordan Abel
    Mar 28, 2006
  5. minlearn
    Replies:
    2
    Views:
    464
    red floyd
    Mar 13, 2009
Loading...

Share This Page