hiding structure members

M

Madhav

Hi all,
I was going through a piece of code which had a very
interesting format. There were two files: one was a .h file, and the
other was a .c file. The .c file had a structure defined in it which
was typedef'ed in .h file.

what I observered was even more interesting: I was allowed
to declare objects of structure that was typedefed in the header file,
but I could not use any of the members of the structure outside the .c
file which delcared it. Every time I did that, I got error:
"dereferencing pointer to incomplete type".

Why is it so that I was not allowed to access the members
but I was allowed to use structure definition in calls to malloc()?
does this problem exists because the structure definition was not
available in the header file? Or is this a neat trick to hide the
structure members?

Please clarify me on this issue. Thanks in advance for your
help.

Madhav.
 
B

Bilgehan.Balban

Madhav said:
Hi all,
I was going through a piece of code which had a very
interesting format. There were two files: one was a .h file, and the
other was a .c file. The .c file had a structure defined in it which
was typedef'ed in .h file.

what I observered was even more interesting: I was allowed
to declare objects of structure that was typedefed in the header file,
but I could not use any of the members of the structure outside the .c
file which delcared it. Every time I did that, I got error:
"dereferencing pointer to incomplete type".
Madhav.

Please give a specific example rather than telling it like a story so
that people can judge what's going on.

Bahadir
 
T

Tydr Schnubbis

Madhav said:
Hi all,
I was going through a piece of code which had a very
interesting format. There were two files: one was a .h file, and the
other was a .c file. The .c file had a structure defined in it which
was typedef'ed in .h file.

what I observered was even more interesting: I was allowed
to declare objects of structure that was typedefed in the header file,
but I could not use any of the members of the structure outside the .c
file which delcared it. Every time I did that, I got error:
"dereferencing pointer to incomplete type".

Why is it so that I was not allowed to access the members
but I was allowed to use structure definition in calls to malloc()?
does this problem exists because the structure definition was not
available in the header file? Or is this a neat trick to hide the
structure members?
You can't use malloc without knowing the size of the struct. Let's say
you have a this in you .h file:

typedef struct my_struct my_struct_t;

And you do this in a .c file:

malloc(sizeof(my_struct_t));

which wouldn't compile. sizeof needs the definition to work. So what
you're saying can't happen. You probably have the definition in scope
without knowing it. Or the typedef is a pointer, like 'typedef struct
my_struct *my_struct_p', in which case you'd be malloc'ing memory only
for a pointer, not for the struct itself.

But this trick could indeed be used to allow you to use a struct without
knowing it's definition (members, fields). But you could only use it
through pointers, and wouldn't be able to allocate new instances yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top