Simple Pointer Question

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

    Simple Pointer Question


    Hi all,



    I have got a pointer question. I was told that a pointer is a variable
    holding a memory address.



    SO:

    ...

    //I: a variable holding a memory address

    unsigned char U8 = 0xFFFF;



    //II: a variable holding a memory address

    unsigned char* pU8 = (unsigned char*)0xFFFF;



    printf("U8 = %X \n",U8);

    printf("pU8 = %X \n",pU8);

    ...



    Output:

    U8 = FF

    pU8 = FFFF



    I understand that U8 = FF (1 byte). I do not understand why pU8 = FFFF
    (2 bytes), there is only 1 byte storage, so where is this number
    (FFFF) stored?


    --
    Posted via http://dbforums.com
  • Gary Labowitz

    #2
    Re: Simple Pointer Question


    "trustron" <member46534@db forums.com> wrote in message
    news:3545277.10 67607076@dbforu ms.com...[color=blue]
    >
    > Hi all,
    >
    >
    >
    > I have got a pointer question. I was told that a pointer is a variable
    > holding a memory address.
    >[/color]
    <<snip wrong stuff>>

    You were told wrong. A pointer is a variable of type pointer that holds an
    address.
    A type defines the size of area in memory used to store a value and the
    encoding of that value. In the case of a pointer it is an area at least
    large enough to store an address for the machine you are on and the encoding
    is the address of another area of memory. It is NOT just bits that look like
    an address.
    There are operations defined for pointer type that only apply to pointer
    variables; like the dereferencing operator and pointer arithmetic.
    --
    Gary


    Comment

    • Gianni Mariani

      #3
      Re: Simple Pointer Question

      trustron wrote:
      [color=blue]
      > ..
      >
      > //I: a variable holding a memory address
      >
      > unsigned char U8 = 0xFFFF;
      >
      > //II: a variable holding a memory address
      >
      > unsigned char* pU8 = (unsigned char*)0xFFFF;
      > printf("U8 = %X \n",U8);
      > printf("pU8 = %X \n",pU8);
      >
      > Output:
      >
      > U8 = FF
      >
      > pU8 = FFFF
      >
      > I understand that U8 = FF (1 byte). I do not understand why pU8 = FFFF
      > (2 bytes), there is only 1 byte storage, so where is this number
      > (FFFF) stored?[/color]


      pU8 is a memory address (i.e. a pointer). Assigning it to FFFF is
      undefined - however on most moderm architectures it is a 32 bit number.
      (hence why you are able to assign 0x0000ffff).

      Comment

      • jeffc

        #4
        Re: Simple Pointer Question


        "Gary Labowitz" <glabowitz@comc ast.net> wrote in message
        news:E6OdnWDvUr nw6z-iRVn-iw@comcast.com. ..[color=blue][color=green]
        > >
        > > I have got a pointer question. I was told that a pointer is a variable
        > > holding a memory address.[/color]
        >
        > You were told wrong. A pointer is a variable of type pointer that holds an
        > address.[/color]

        He was not told wrong* - a pointer *is* a variable holding a memory address.
        (At least it's supposed to - it could well be argued that 0 is not a memory
        address.) He simply wasn't given enough information.

        *I've never seen a newsgroup where people are so eager to tell other people
        how wrong they are.....


        Comment

        • Andrey Tarasevich

          #5
          Re: Simple Pointer Question

          jeffc wrote:[color=blue][color=green][color=darkred]
          >> >
          >> > I have got a pointer question. I was told that a pointer is a variable
          >> > holding a memory address.[/color]
          >>
          >> You were told wrong. A pointer is a variable of type pointer that holds an
          >> address.[/color]
          >
          > He was not told wrong* - a pointer *is* a variable holding a memory address.
          > (At least it's supposed to - it could well be argued that 0 is not a memory
          > address.) He simply wasn't given enough information.[/color]

          There are at least four fundamentally different flavors of pointers in
          C++: regular pointers to data, pointers to regular functions, pointers
          to member functions, pointers to data members. In general case some of
          these kinds of pointers will hold a lot more than just a "memory
          address". For this reason, in general case it is not correct to say that
          a pointer "is a variable holding a memory address".

          A pointer holds an address, all right (keeping in mind that in C++
          terminology term "address" is a synonym for term "pointer"). Once you
          start qualifying that term "address" with such undefined modifiers as
          "memory" (as in your "memory address"), you start walking on thin ice.
          Needlessly, I might add.

          --
          Best regards,
          Andrey Tarasevich

          Comment

          Working...