--- In [email protected], "John Matthews" <[EMAIL PROTECTED]> wrote:
>
> --- In [email protected], "raj_duttaphookan"
> <raj_duttaphookan@> wrote:
> >
> > --- In [email protected], "John Matthews" <jm5678@> wrote:
> > >
> > > --- In [email protected], "raj_duttaphookan"
> > > <raj_duttaphookan@> wrote:
> > > >
> > > > Why can't we simply 
> > > > define it as a pointer. It shall anyway hold the memory 
address 
> > and 
> > > > later on while dereferencing it, it shall automatically 
> > dereference it 
> > > > depending on whether the memory address it contained was 
that of 
> > an 
> > > > int or a float or a char etc.
> > > 
> > > But when dereferencing, how would the compiler know what was 
> > stored in
> > > the memory? It can't just use the destination type. For 
example:
> > > 
> > >     pointer p;
> > >     int i;
> > > 
> > >     i = *p;
> > > 
> > > Does the memory pointed to by p contain a 4 byte integer, a 2 
byte
> > > integer, or a 1 byte character, all of which can be assigned 
to an
> > > integer variable?
> > >
> > I guess the compiler would know it because when we define a 
variable
> > it is assigned a memory address. 
> >   so the compiler already know what is stored at address 10448 
and 
> > what is stored at address 10566. 
> 
> But to handle something like:
> 
> int getValue(pointer p)
> {
>     return *p; // what type?
> }
> 
> the compiler would have to maintain a list of all memory addresses 
and
> the type of information in them.
> 
> John
>
Well, you do have a point, but I am not convienced.

In your above explaination, I guess when the getValue( ) function 
shall be called, it shall be something like:

main( )
{
   int x;
   ..............;
   ..............;
   int getValue(&x);//the address of x is passed on to the calling 
                     //function.
   ..............;
}


int getValue(pointer p) //here the address shall come to p
 {
     return *p; // here it is simply returning the value at the 
                //address which is contained in p. And since it
                //was already defined in main( ) as int x, so 
                //obviously it shall return an int.  
 }



Reply via email to