A
Alex Vinokur
[snip]Richard Bos said:(e-mail address removed) (Ben) wrote: [snip]2) Structure casted into an array of char
typedef struct {
char name[20];
int age;
int id;
} person;
person p = (person *) malloc(sizeof(person));
p.name="hello";
p.age=2;
p.id=2;
char *a;
a=(char *)p;
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!
Is it possible to write 'operator char*()' inside the C++-structure that enables to avoid the padding problem?