469,934 Members | 2,479 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

hiding structure members

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.

Nov 15 '05 #1
2 2321
Madhav wrote:
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

Nov 15 '05 #2
Madhav wrote:
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.
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by thechaosengine | last post: by
11 posts views Thread by Lorenzo Villari | last post: by
15 posts views Thread by damian birchler | last post: by
4 posts views Thread by Sharon Tal | last post: by
7 posts views Thread by mathieu | last post: by
11 posts views Thread by sofeng | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.