This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Assign pointers of different objects to the same pointer
sebastian writes:
>
> > > (That's pointers get handled in other languages, without a standard for
> > > objective oriented programming)
> > > But a pointer can point to whatever you want
> >
> > No, it can't. Not in C or C++ it can't. A pointer can only point to
> > a compatible type. Anything else is undefined.
> Surely not c and c++ but generally or in other languages and for the CPU (ptr)
> are all pointers the same.
I think you're missing something important.
gcc and many other compilers will not do what you expect if you make a
pointer point to a type other than a compatible type. Consider this
function:
int poo (int *a, short *p)
{
*a = 99;
*p = 5;
if (*a != 99)
{
// Do stuff
}
}
gcc is perfectly entitled to translate this as:
int poo (int *a, short *p)
{
*p = 5;
*a = 99;
}
Andrew.