Querry on pointers

Discussion in 'C Programming' started by abhaybhat, Jun 12, 2008.

  1. abhaybhat

    abhaybhat Guest

    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
    abhaybhat, Jun 12, 2008
    #1
    1. Advertising

  2. abhaybhat

    Chris Dollin Guest

    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
    Chris Dollin, Jun 12, 2008
    #2
    1. Advertising

  3. abhaybhat

    santosh Guest

    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
    implementability 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>
    santosh, Jun 12, 2008
    #3
  4. abhaybhat

    Guest

    On Jun 12, 3:38 pm, Chris Dollin <> wrote:
    > 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)
    , Jun 12, 2008
    #4
  5. On Thu, 12 Jun 2008 05:09:39 -0700 (PDT), abhaybhat
    <> wrote:

    >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
    Barry Schwarz, Jun 12, 2008
    #5
  6. abhaybhat

    Guest

    On Jun 12, 4:07 pm, Barry Schwarz <> wrote:
    > On Thu, 12 Jun 2008 05:09:39 -0700 (PDT), abhaybhat
    >
    > <> wrote:
    > >Hello all,

    >
    > >char a[2]
    > >char* u8
    > >int u32

    >
    > >u8 = &u32

    >
    > >Cant we referance a char pointer to int ?

    >
    > Not implicitly.

    char *p = 0;
    , Jun 12, 2008
    #6
  7. On 12 Jun, 13:09, abhaybhat <> wrote:

    > 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?
    Nick Keighley, Jun 12, 2008
    #7
  8. abhaybhat

    Chris Dollin Guest

    wrote:

    > On Jun 12, 4:07 pm, Barry Schwarz <> wrote:
    >> On Thu, 12 Jun 2008 05:09:39 -0700 (PDT), abhaybhat
    >>
    >> <> wrote:
    >> >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
    Chris Dollin, Jun 12, 2008
    #8
  9. abhaybhat

    Chris Dollin Guest

    wrote:

    > On Jun 12, 3:38 pm, Chris Dollin <> wrote:
    >> 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
    Chris Dollin, Jun 12, 2008
    #9
  10. abhaybhat

    Guest

    On Jun 12, 4:27 pm, Chris Dollin <> wrote:
    > wrote:
    > > On Jun 12, 3:38 pm, Chris Dollin <> wrote:
    > >> 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.h> and 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.
    , Jun 12, 2008
    #10
  11. abhaybhat

    Chris Dollin Guest

    wrote:

    > On Jun 12, 4:27 pm, Chris Dollin <> wrote:
    >> wrote:
    >> > On Jun 12, 3:38 pm, Chris Dollin <> wrote:
    >> >> 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.h> and 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
    Chris Dollin, Jun 12, 2008
    #11
  12. santosh <> writes:
    > 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
    > implementability 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_Keith) <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"
    Keith Thompson, Jun 12, 2008
    #12
    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. Graham

    feedback form querry

    Graham, Feb 12, 2004, in forum: HTML
    Replies:
    1
    Views:
    422
    Leif K-Brooks
    Feb 12, 2004
  2. SpaceGirl

    Parse querry string with ssi

    SpaceGirl, May 22, 2004, in forum: HTML
    Replies:
    2
    Views:
    1,162
    Martin Johansen
    May 23, 2004
  3. Maulik Thaker

    querry on ID's and IDREF in DTD

    Maulik Thaker, Feb 21, 2006, in forum: XML
    Replies:
    10
    Views:
    1,228
    Aalap
    Feb 25, 2006
  4. gurpreet

    querry about compiling and linking

    gurpreet, Feb 28, 2005, in forum: C Programming
    Replies:
    3
    Views:
    285
    osmium
    Feb 28, 2005
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    657
Loading...

Share This Page