Re: hiding structure members



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.
.




Relevant Pages

  • Re: Variadic functions calling variadic functions with the argument list, HLL bit shifts on LE proce
    ... then I get to thinking about memory and memory allocation. ... typedef unsigned int fc_ptr_int_t; ... /** This is the static cast for void pointer to typed pointer, ...
    (comp.lang.c)
  • Re: RE:How to realize the OOP in C?
    ... > int n; ... be a pointer to a standard structure like: ... typedef struct object_of_fubar * ObjectOfFubarP; ... Note that the class structure is allocated only ...
    (comp.lang.c)
  • Re: Typdef pointers to structs or not? [from clc]
    ... be treated as a purely abstract type. ... then the type should visibley be a pointer type. ... If, on the other hand, the interface is restricted to things like ... Early C didn't have either typedef or the ability to pass structures ...
    (comp.std.c)
  • Re: Derived type - valued function with pointer member.
    ... If I understand what you want to do, the only reason you need the pointer attribute is because the allocatable attribute for derived type components didn't make it into f95. ... When I make the %vec's allocatable arrays instead, the program segfaults on calling vplus. ... I think the safer route is to create specific allocate and destroy functions for the vector data type and use those. ... Default is to test ALL the pointer members ...
    (comp.lang.fortran)
  • Re: Typdef pointers to structs or not?
    ... typedef struct exp_ { ... structs ... modify these structs so either the whole typedef will be in a .h file ... a pointer. ...
    (comp.lang.c)