Querry on pointers

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

    Querry on pointers

    Hello all,

    char a[2]
    char* u8
    int u32

    u8 = &u32

    Cant we referance a char pointer to int ?
    Why is this not possible?

    I want to transfer all the data from array a to u32.


    Thanks


  • Chris Dollin

    #2
    Re: Querry on pointers

    abhaybhat wrote:
    Hello all,
    >
    char a[2]
    char* u8
    int u32
    >
    u8 = &u32
    You want to assign an int* to a char*? That sounds
    like a recipe for confusion.
    Cant we referance a char pointer to int ?
    Why is this not possible?
    It's /possible/. It's likely not /wise/, depending.
    I want to transfer all the data from array a to u32.
    u32 = (a[1] << 8) + a[0];

    (where `8` should likely be spelled `CHAR_BIT` and `1`
    and `0` may need interchanging depending on what
    endianness you've picked for the bytes.)

    No pointer-type-games needed.

    --
    "Where the shadows run from themselves." /White Room/

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

    Comment

    • santosh

      #3
      Re: Querry on pointers

      abhaybhat wrote:
      Hello all,
      >
      char a[2]
      char* u8
      int u32
      >
      u8 = &u32
      >
      Cant we referance a char pointer to int ?
      Not portably. A pointer of type T* may hold either the value NULL or the
      address of an object of type T. The special pointer void* may hold NULL
      or the address of any object type, but may not be deferenced. Nor can
      you do pointer arithmetic on void*.
      Why is this not possible?
      That's how the language has been designed. The main reason is
      implementabilit y on systems where different pointer types may be
      incompatible with each other, i.e., their size may be different, their
      representation may be different, etc.

      <snip>

      Comment

      • vippstar@gmail.com

        #4
        Re: Querry on pointers

        On Jun 12, 3:38 pm, Chris Dollin <chris.dol...@h p.comwrote:
        abhaybhat wrote:
        Hello all,
        >
        char a[2]
        char* u8
        int u32
        >
        u8 = &u32
        >
        You want to assign an int* to a char*? That sounds
        like a recipe for confusion.
        >
        Cant we referance a char pointer to int ?
        Why is this not possible?
        >
        It's /possible/. It's likely not /wise/, depending.
        >
        I want to transfer all the data from array a to u32.
        >
        u32 = (a[1] << 8) + a[0];
        >
        (where `8` should likely be spelled `CHAR_BIT` and `1`
        and `0` may need interchanging depending on what
        endianness you've picked for the bytes.)
        AFAIK that won't work if int's size is char's size as well. (*if* 8 is
        replaced with CHAR_BIT)

        Comment

        • Barry Schwarz

          #5
          Re: Querry on pointers

          On Thu, 12 Jun 2008 05:09:39 -0700 (PDT), abhaybhat
          <abhayjb@gmail. comwrote:
          >Hello all,
          >
          >char a[2]
          >char* u8
          >int u32
          >
          >u8 = &u32
          >
          >Cant we referance a char pointer to int ?
          Not implicitly.
          >Why is this not possible?
          It is possible; use a cast.
          >
          >I want to transfer all the data from array a to u32.
          Use memcpy. But why? Is there any guarantee that the result will be
          in any way meaningful? Based on you names, it looks like a will
          occupy only half of u32. What will you do with the other half?

          You could put a and u32 in a union and not need to transfer anything.


          Remove del for email

          Comment

          • vippstar@gmail.com

            #6
            Re: Querry on pointers

            On Jun 12, 4:07 pm, Barry Schwarz <schwa...@dqel. comwrote:
            On Thu, 12 Jun 2008 05:09:39 -0700 (PDT), abhaybhat
            >
            <abha...@gmail. comwrote:
            Hello all,
            >
            char a[2]
            char* u8
            int u32
            >
            u8 = &u32
            >
            Cant we referance a char pointer to int ?
            >
            Not implicitly.
            char *p = 0;

            Comment

            • Nick Keighley

              #7
              Re: Querry on pointers

              On 12 Jun, 13:09, abhaybhat <abha...@gmail. comwrote:
              char a[2]
              char* u8
              int u32
              I'm never too fond of things like u8 and u32,
              but if you must use them then most people would expect
              the "u" to stand for unsigned. char may or may not be unsigned
              int is never unsigned.

              If u8 is 8-bits and u32 is 32-bits then a u32 will
              consist of 4 u8s not 2 as you imply.

              u8 = &u32
              >
              Cant we referance a char pointer to int ?
              Why is this not possible?
              >
              I want to transfer all the data from array a to u32.
              I bet you don't.


              --
              Nick Keighley

              why isn't there an obfuscated C++ contest?

              Comment

              • Chris Dollin

                #8
                Re: Querry on pointers

                vippstar@gmail. com wrote:
                On Jun 12, 4:07 pm, Barry Schwarz <schwa...@dqel. comwrote:
                >On Thu, 12 Jun 2008 05:09:39 -0700 (PDT), abhaybhat
                >>
                ><abha...@gmail .comwrote:
                >Hello all,
                >>
                >char a[2]
                >char* u8
                >int u32
                >>
                >u8 = &u32
                >>
                >Cant we referance a char pointer to int ?
                >>
                >Not implicitly.
                char *p = 0;
                That's not an example of "referance a char pointer to int"
                (by which the OP seems to mean "assign an int pointer value
                to a char pointer variable").

                --
                "The letter was not unproductive." /Mansfield Park/

                Hewlett-Packard Limited registered no:
                registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

                Comment

                • Chris Dollin

                  #9
                  Re: Querry on pointers

                  vippstar@gmail. com wrote:
                  On Jun 12, 3:38 pm, Chris Dollin <chris.dol...@h p.comwrote:
                  >abhaybhat wrote:
                  Hello all,
                  >>
                  char a[2]
                  char* u8
                  int u32
                  >>
                  u8 = &u32
                  >>
                  >You want to assign an int* to a char*? That sounds
                  >like a recipe for confusion.
                  >>
                  Cant we referance a char pointer to int ?
                  Why is this not possible?
                  >>
                  >It's /possible/. It's likely not /wise/, depending.
                  >>
                  I want to transfer all the data from array a to u32.
                  >>
                  > u32 = (a[1] << 8) + a[0];
                  >>
                  >(where `8` should likely be spelled `CHAR_BIT` and `1`
                  > and `0` may need interchanging depending on what
                  > endianness you've picked for the bytes.)
                  AFAIK that won't work if int's size is char's size as well. (*if* 8 is
                  replaced with CHAR_BIT)
                  If `sizeof(int)` equals `sizeof(char)`, what the OP wants to do
                  cannot be done at all (and their chosen names would be misleading).
                  Quarts & pint pots.

                  --
                  "Giving my opinion / to whoever's there." /Wonderland/

                  Hewlett-Packard Limited registered office: Cain Road, Bracknell,
                  registered no: 690597 England Berks RG12 1HN

                  Comment

                  • vippstar@gmail.com

                    #10
                    Re: Querry on pointers

                    On Jun 12, 4:27 pm, Chris Dollin <chris.dol...@h p.comwrote:
                    vipps...@gmail. com wrote:
                    On Jun 12, 3:38 pm, Chris Dollin <chris.dol...@h p.comwrote:
                    abhaybhat wrote:
                    Hello all,
                    >
                    char a[2]
                    char* u8
                    int u32
                    >
                    u8 = &u32
                    >
                    You want to assign an int* to a char*? That sounds
                    like a recipe for confusion.
                    >
                    Cant we referance a char pointer to int ?
                    Why is this not possible?
                    >
                    It's /possible/. It's likely not /wise/, depending.
                    >
                    I want to transfer all the data from array a to u32.
                    >
                    u32 = (a[1] << 8) + a[0];
                    >
                    (where `8` should likely be spelled `CHAR_BIT` and `1`
                    and `0` may need interchanging depending on what
                    endianness you've picked for the bytes.)
                    AFAIK that won't work if int's size is char's size as well. (*if* 8 is
                    replaced with CHAR_BIT)
                    >
                    If `sizeof(int)` equals `sizeof(char)`, what the OP wants to do
                    cannot be done at all (and their chosen names would be misleading).
                    Quarts & pint pots.
                    Their names are misleading nonetheless.
                    Using <stdint.hand uintN_t it can be done:

                    uint8_t arrayu8[2];
                    /*...*/
                    uint32_t u32 = arrayu8[1] << 8 | arrrayu8[0];

                    It is not however portable to use << CHAR_BIT or >CHAR_BIT with any
                    integer expression.

                    Comment

                    • Chris Dollin

                      #11
                      Re: Querry on pointers

                      vippstar@gmail. com wrote:
                      On Jun 12, 4:27 pm, Chris Dollin <chris.dol...@h p.comwrote:
                      >vipps...@gmail .com wrote:
                      On Jun 12, 3:38 pm, Chris Dollin <chris.dol...@h p.comwrote:
                      >abhaybhat wrote:
                      Hello all,
                      >>
                      char a[2]
                      char* u8
                      int u32
                      >>
                      u8 = &u32
                      >>
                      >You want to assign an int* to a char*? That sounds
                      >like a recipe for confusion.
                      >>
                      Cant we referance a char pointer to int ?
                      Why is this not possible?
                      >>
                      >It's /possible/. It's likely not /wise/, depending.
                      >>
                      I want to transfer all the data from array a to u32.
                      >>
                      > u32 = (a[1] << 8) + a[0];
                      >>
                      >(where `8` should likely be spelled `CHAR_BIT` and `1`
                      > and `0` may need interchanging depending on what
                      > endianness you've picked for the bytes.)
                      AFAIK that won't work if int's size is char's size as well. (*if* 8 is
                      replaced with CHAR_BIT)
                      >>
                      >If `sizeof(int)` equals `sizeof(char)`, what the OP wants to do
                      >cannot be done at all (and their chosen names would be misleading).
                      >Quarts & pint pots.
                      Their names are misleading nonetheless.
                      Using <stdint.hand uintN_t it can be done:
                      >
                      uint8_t arrayu8[2];
                      /*...*/
                      uint32_t u32 = arrayu8[1] << 8 | arrrayu8[0];
                      >
                      It is not however portable to use << CHAR_BIT or >CHAR_BIT with any
                      integer expression.
                      When those expressions are not defined, the OP can't do what they
                      want anyway. When they can do what they want (or at least my
                      reading of it), the shifts are well-defined.

                      I agree that the code must be guarded somehow against undefinedness.

                      --
                      "I am trying to say that you have no choice." /The Courts of Chaos/

                      Hewlett-Packard Limited Cain Road, Bracknell, registered no:
                      registered office: Berks RG12 1HN 690597 England

                      Comment

                      • Keith Thompson

                        #12
                        Re: Querry on pointers

                        santosh <santosh.k83@gm ail.comwrites:
                        abhaybhat wrote:
                        >
                        >Hello all,
                        >>
                        >char a[2]
                        >char* u8
                        >int u32
                        >>
                        >u8 = &u32
                        >>
                        >Cant we referance a char pointer to int ?
                        >
                        Not portably. A pointer of type T* may hold either the value NULL or the
                        address of an object of type T. The special pointer void* may hold NULL
                        or the address of any object type, but may not be deferenced. Nor can
                        you do pointer arithmetic on void*.
                        >
                        >Why is this not possible?
                        >
                        That's how the language has been designed. The main reason is
                        implementabilit y on systems where different pointer types may be
                        incompatible with each other, i.e., their size may be different, their
                        representation may be different, etc.
                        I think the main reason is simple type safety, so programmers don't go
                        around assigning float values to int objects and so forth. (You can
                        still do such things, but it's a bit more complicated, and the
                        gyrations you have to go through make it a bit clearer that it's
                        non-portable.)

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        Nokia
                        "We must do something. This is something. Therefore, we must do this."
                        -- Antony Jay and Jonathan Lynn, "Yes Minister"

                        Comment

                        Working...