"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.1 97032434@news.i ndividual.net.. . to news:comp.lang. c[color=blue]
> ben19777@hotmai l.com (Ben) wrote:[/color]
[snip][color=blue][color=green]
> > 2) Structure casted into an array of char
> > typedef struct {
> > char name[20];
> > int age;
> > int id;
> > } person;
> >
> > person p = (person *) malloc(sizeof(p erson));
> > p.name="hello";
> > p.age=2;
> > p.id=2;
> >
> > char *a;
> > a=(char *)p;[/color]
>
> No. You cannot cast a struct into a pointer. And what would you do this
> for, anyway? Do you suppose that the bytes represented by "hello"
> somehow form a valid pointer?
> I suspect you meant to access the struct itself _through_, not _as_, a
> pointer to char. In that case, you need to do
>
> a = (char *)&p;
>
> which is perfectly legal, and can be useful - though rarely. Beware the
> padding bytes!
>[/color]
[snip]
Is it possible to write 'operator char*()' inside the C++-structure that enables to avoid the padding problem?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://sourceforge.net/users/alexvn
Comment