Newbie: pointer questions.

Z

Zalek Bloom

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
 
A

Alf P. Steinbach

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


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.
 
R

Rob Williscroft

Zalek Bloom wrote in
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.
 
J

John Harrison

Zalek Bloom said:
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
 
G

Greg Comeau

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


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.
 
O

osmium

Zalek said:
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.
 
P

PT

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[]"
/////////////////////////////////////////////////////////////
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top