Newbie: pointer questions.

Discussion in 'C++' started by Zalek Bloom, Aug 17, 2003.

  1. Zalek Bloom

    Zalek Bloom Guest

    I understand that a pointer is a variable that hold an address, so why
    we have to specify the type of variable of the pointer, for example:

    int * age ;
    float * price ;

    etc.

    If a pointer holds an address, why do we need to specify int, float
    and other for a pointer?

    Thanks,

    Zalek
     
    Zalek Bloom, Aug 17, 2003
    #1
    1. Advertising

  2. [Please do not crosspost to both C and C++ groups, it can be confusing.]


    On Sun, 17 Aug 2003 20:40:11 GMT, Zalek Bloom <> wrote:

    >I understand that a pointer is a variable that hold an address, so why
    >we have to specify the type of variable of the pointer, for example:
    >
    >int * age ;
    >float * price ;
    >
    >etc.
    >
    >If a pointer holds an address, why do we need to specify int, float
    >and other for a pointer?


    In order to specify the size of the memory area, how the memory
    contents at that address should be interpreted, and what operations
    should be available.

    E.g.,

    *age

    is an 'int' variable, and

    *price

    is a 'float' variable, assuming valid adresses in the pointers.
     
    Alf P. Steinbach, Aug 17, 2003
    #2
    1. Advertising

  3. Zalek Bloom wrote in news::

    > I understand that a pointer is a variable that hold an address, so why
    > we have to specify the type of variable of the pointer, for example:
    >
    > int * age ;
    > float * price ;
    >
    > etc.
    >
    > If a pointer holds an address, why do we need to specify int, float
    > and other for a pointer?
    >


    So when we want to use the value that the pointer points to, the
    compiler will know how to get at it for us.

    int f( int * p )
    {
    return *p;
    }

    There is a "generic" pointer type in C++ void *, but we can't use
    this address to retrieve values directly.

    int f2( void * p )
    {
    return *static_cast< int * >( p );
    }

    Note that although we can write code like f2() above, it less
    "type safe", i.e.. the compiler isn't watching are backs and
    checking that p *really" does point to an int.

    HTH

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Aug 17, 2003
    #3
  4. "Zalek Bloom" <> wrote in message
    news:...
    > I understand that a pointer is a variable that hold an address, so why
    > we have to specify the type of variable of the pointer, for example:
    >
    > int * age ;
    > float * price ;
    >
    > etc.
    >
    > If a pointer holds an address, why do we need to specify int, float
    > and other for a pointer?
    >
    > Thanks,
    >
    > Zalek


    So that the compiler knows what the pointer is pointing at, this will be
    important if you ever decide to access what the pointer is pointing at. If
    you want a pure address use void*,

    void* an_address;

    john
     
    John Harrison, Aug 17, 2003
    #4
  5. Zalek Bloom

    Greg Comeau Guest

    In article <>,
    Alf P. Steinbach <> wrote:
    >[Please do not crosspost to both C and C++ groups, it can be confusing.]
    >
    >
    >On Sun, 17 Aug 2003 20:40:11 GMT, Zalek Bloom <> wrote:
    >
    >>I understand that a pointer is a variable that hold an address, so why
    >>we have to specify the type of variable of the pointer, for example:
    >>
    >>int * age ;
    >>float * price ;
    >>
    >>etc.
    >>
    >>If a pointer holds an address, why do we need to specify int, float
    >>and other for a pointer?

    >
    >In order to specify the size of the memory area, how the memory
    >contents at that address should be interpreted, and what operations
    >should be available.
    >
    >E.g.,
    >
    > *age
    >
    >is an 'int' variable, and
    >
    > *price
    >
    >is a 'float' variable, assuming valid adresses in the pointers.


    Right, if not, the pointer type would have to "carry around"
    with it, what type of object it is pointing to, and that's
    considered burdensome. Similarly, as well, some architectures
    support pointers to different types and can use them more
    optimally.
    --
    Greg Comeau/4.3.3:Full C++03 core language + more Windows backends
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Aug 17, 2003
    #5
  6. Zalek Bloom

    osmium Guest

    Zalek Bloom writes:

    > I understand that a pointer is a variable that hold an address, so why
    > we have to specify the type of variable of the pointer, for example:
    >
    > int * age ;
    > float * price ;
    >
    > etc.
    >
    > If a pointer holds an address, why do we need to specify int, float
    > and other for a pointer?


    Because one of the operations that can be performed on a pointer is
    addition. The compiler needs to know how much to add (in bytes), so that
    after an add of 1 unit, the pointer will be pointing at the next char, int,
    float, whatever.
     
    osmium, Aug 18, 2003
    #6
  7. Zalek Bloom

    PT Guest

    > I understand that a pointer is a variable that hold an address, so why
    > we have to specify the type of variable of the pointer, for example:
    >
    > int * age ;
    > float * price ;
    >
    > etc.
    >
    > If a pointer holds an address, why do we need to specify int, float
    > and other for a pointer?


    It is necessary because if you want to write something to the memory area
    the pointer points to, then the computer needs to know, how many bytes do
    you want to write. The pointer by itself does not contain any information
    about the length of the memory area allocated.

    Of course, there is also a possibility for non-type pointers - they are
    defined as of type "void *". But if you assign something to the area they
    point to, then you *must* cast the types:
    ////////////////////////////////////////////////////////////
    void *pointer; //Let's create a new void pointer
    pointer=new int[6]; //Allocate memory for 6 integers and store the pointer
    //to the beginning of the array in "pointer"
    ((int *)pointer)[3]=654; //Let the third element of the array be 654
    //Note the typecasting!
    //Otherwise the computer wouldn't know exactly, where do you
    //want to write the number.
    //If, for instance, we would have an array of char's, then the third
    //element would be only the 3. byte from the "*pointer".
    //But for int's it is "3*sizeof(int)"'s byte (sizeof(int) is normally
    //4. or 2. on older compiler or a 16-bit architecture)

    // Now do anything you like
    delete[] (int *)pointer; //No let's free the array, again note the casting!

    pointer=new char; //Now let's use *pointer as a single character
    *((char *)pointer)='a'; //Or anything you like
    delete (char *)a; //And finally let's free the memory (because we have a
    //single variable at "*pointer", we now use "delete", not "delete[]"
    /////////////////////////////////////////////////////////////
     
    PT, Aug 25, 2003
    #7
    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. Replies:
    10
    Views:
    707
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    846
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,276
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    639
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    460
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page