generic pointer question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Logan Lee

    generic pointer question

    int
    main()
    {
    int i;
    char c;
    void *the_data;

    i = 6;
    c = 'a';

    the_data = &i;
    printf("the_dat a points to the integer value %d\n", *(int*) the_data);
    ^^^^^^^
    the_data = &c;
    printf("the_dat a 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
  • Richard Heathfield

    #2
    Re: generic pointer question

    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

    Comment

    • Logan Lee

      #3
      Re: generic pointer question

      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_dat a points to the integer value %d\n", *(int*) the_data);
      ^^^^^^^
      the_data = &c;
      printf("the_dat a 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?

      Comment

      • Richard Heathfield

        #4
        Re: generic pointer question

        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

        Comment

        • Logan Lee

          #5
          Re: generic pointer question

          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?

          Comment

          • Joachim Schmitz

            #6
            Re: generic pointer question

            "Logan Lee" <logan.w.lee@st udent.uts.edu.a uschrieb im Newsbeitrag
            news:4774dc03$0 $13262$afc38c87 @news.optusnet. com.au...
            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


            Comment

            • Richard Heathfield

              #7
              Re: generic pointer question

              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

              Comment

              • James Kuyper

                #8
                Re: generic pointer question

                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.

                Comment

                • Keith Thompson

                  #9
                  Re: generic pointer question

                  Richard Heathfield <rjh@see.sig.in validwrites:
                  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_c ast" -- or is it
                  "reinterpret_ca st"? 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_Keit h) <kst-u@mib.org>
                  [...]
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • Richard Heathfield

                    #10
                    Re: generic pointer question

                    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

                    Comment

                    • steve.pagliarulo@gmail.com

                      #11
                      Re: generic pointer question

                      On Dec 28, 2:50 am, Logan Lee <logan.w....@st udent.uts.edu.a uwrote:
                      int
                      main()
                      {
                        int i;
                        char c;
                        void *the_data;
                      >
                        i = 6;
                        c = 'a';
                      >
                        the_data = &i;
                        printf("the_dat a points to the integer value %d\n", *(int*) the_data);
                                                                            ^^^^^^^
                        the_data = &c;
                        printf("the_dat a 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.

                      Comment

                      • CBFalconer

                        #12
                        Re: generic pointer question

                        Logan Lee wrote:
                        >
                        int main() {
                        int i;
                        char c;
                        void *the_data;
                        >
                        i = 6;
                        c = 'a';
                        the_data = &i;
                        printf("the_dat a points to the integer value %d\n", *(int*) the_data);
                        the_data = &c;
                        printf("the_dat a 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_da ta is integer value %d\n", *(int*)the_data );
                        the_data = &c;
                        printf("*the_da ta now is the char %c\n", *(char*)the_dat a);
                        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

                        Comment

                        • CBFalconer

                          #13
                          Re: generic pointer question

                          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

                          Comment

                          • Keith Thompson

                            #14
                            Re: generic pointer question

                            steve.pagliarul o@gmail.com writes:
                            On Dec 28, 2:50 am, Logan Lee <logan.w....@st udent.uts.edu.a uwrote:
                            >int
                            >main()
                            >{
                            >  int i;
                            >  char c;
                            >  void *the_data;
                            >>
                            >  i = 6;
                            >  c = 'a';
                            >>
                            >  the_data = &i;
                            >  printf("the_dat a points to the integer value %d\n", *(int*) the_data);
                            >                                                      ^^^^^^^
                            >  the_data = &c;
                            >  printf("the_dat a 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_Keit h) <kst-u@mib.org>
                            [...]
                            "We must do something. This is something. Therefore, we must do this."
                            -- Antony Jay and Jonathan Lynn, "Yes Minister"

                            Comment

                            • spaglia

                              #15
                              Re: generic pointer question

                              On Dec 28, 8:38 pm, Keith Thompson <ks...@mib.orgw rote:
                              steve.pagliar.. .@gmail.com writes:
                              On Dec 28, 2:50 am, Logan Lee <logan.w....@st udent.uts.edu.a uwrote:
                              int
                              main()
                              {
                                int i;
                                char c;
                                void *the_data;
                              >
                                i = 6;
                                c = 'a';
                              >
                                the_data = &i;
                                printf("the_dat a points to the integer value %d\n", *(int*) the_data);
                                                                                   ^^^^^^^
                                the_data = &c;
                                printf("the_dat a 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_Keit h) <ks...@mib.or g>
                              [...]
                              "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.

                              Comment

                              Working...