Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > hiding structure members

Reply
Thread Tools

hiding structure members

 
 
Madhav
Guest
Posts: n/a
 
      06-27-2005
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.

 
Reply With Quote
 
 
 
 
Bilgehan.Balban@gmail.com
Guest
Posts: n/a
 
      06-27-2005
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

 
Reply With Quote
 
 
 
 
Tydr Schnubbis
Guest
Posts: n/a
 
      06-27-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hiding members using `using' Paul Bibbings C++ 3 06-05-2010 02:07 PM
Script for Hiding/Un-Hiding Text On Click Ste Javascript 41 08-01-2007 02:35 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
Hiding the public members of a class without modifying the CLASS Brahmam ASP .Net 3 01-11-2006 12:00 AM
Hiding public members of a base class Ombra ASP .Net Web Controls 1 06-04-2004 03:26 PM



Advertisments