hiding structure members

Discussion in 'C Programming' started by Madhav, Jun 27, 2005.

  1. Madhav

    Madhav Guest

    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.
     
    Madhav, Jun 27, 2005
    #1
    1. Advertising

  2. Madhav

    Guest

    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
     
    , Jun 27, 2005
    #2
    1. Advertising

  3. 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.
     
    Tydr Schnubbis, Jun 27, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brahmam
    Replies:
    3
    Views:
    560
    Francois Beaussier
    Jan 11, 2006
  2. JFCM
    Replies:
    4
    Views:
    5,739
  3. Paul Bibbings

    Hiding members using `using'

    Paul Bibbings, Jun 5, 2010, in forum: C++
    Replies:
    3
    Views:
    358
    Juha Nieminen
    Jun 5, 2010
  4. Ombra

    Hiding public members of a base class

    Ombra, Jun 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    120
    Mythran
    Jun 4, 2004
  5. Ste
    Replies:
    41
    Views:
    810
    Thomas 'PointedEars' Lahn
    Aug 1, 2007
Loading...

Share This Page