473,395 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2448
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: thechaosengine | last post by:
Hi all, Is there a way to hide a member in a subclass that has been inherited from a base class? Lets leave aside any issues regarding whether its a good idea for a moment. Here's an example...
11
by: Lorenzo Villari | last post by:
I premise I don't know C++ well but... I wondered what is this data hiding thing... I mean, if I can look at the header (and i need it beacuse of the class), then what's hidden? Can someone give...
13
by: John | last post by:
In the course of an assignment, I learned the hard way that I shouldn't try to free a malloc'd member of a malloc'd structure after having freed that structure (i.e., free( structure ); free(...
15
by: damian birchler | last post by:
Hi I'm wondering of what type a structure is. Of course, it is a _structure_, but an array isn't an _array_ either. So of what type is a structure? I'd say a pointer, am I right?
4
by: Sharon Tal | last post by:
Hi all. I am trying to figure out the differences between overriding and hiding a method name. The only difference i can see, is that with name hiding i can change the method access level. Are...
7
by: mathieu | last post by:
Hello, I did read the FAQ on template(*), since I could not find an answer to my current issue I am posting here. I have tried to summarize my issue in the following code (**). Basically I am...
11
by: sofeng | last post by:
I'm not sure if "data hiding" is the correct term, but I'm trying to emulate this object-oriented technique. I know C++ probably provides much more than my example, but I'd just like some feedback...
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.