--- In [email protected], "John Matthews" <[EMAIL PROTECTED]> 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? > > John > I guess the compiler would know it because when we define a variable it is assigned a memory address. E.g.: int x; //memory address say 10448 is assigned to x float y; //memory address say 10566 is assigned to y pointer*p; //Defining a free type of pointer pointer*q; //defining a free type of pointer p=&x; //here the memory address 10448 is passed on to p q=&y; //here the memory address 10566 is passed on to q
so the compiler already know what is stored at address 10448 and what is stored at address 10566. Later on we shall (as usual)use the following only: p->x=5; q->y=4.16; and not the other way round.
