Thread: Hiding Structure members

  1. #1
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    64

    Hiding Structure members

    I want to include the header file linked_list.h into main and I want to have the structure LINKED_LIST available in main but not its members(NODE_PTR head, NODE_PTR tail). How do I achieve that?

    linked_list.h
    Code:
    #ifndef LINKED_LIST__HH
    #define LINKED_LIST__HH
    #include "node.h"
    
    typedef struct linked_list {
      NODE_PTR head;
      NODE_PTR tail;
    } LINKED_LIST;
    
    void add_head(LINKED_LIST*, int);
    void add_tail(LINKED_LIST*, int);
    void display_linked_list(LINKED_LIST);
    void free_linked_list(LINKED_LIST*);
    LINKED_LIST create_linked_list();
    
    #endif
    Basically I want to hide the details of the structure LINKED_LIST.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,411
    The typical approach is to provide an opaque pointer. But this means that all the functions that make up the interface of your library must either accept a pointer parameter or return a pointer that is never dereferenced by the caller.

    So for example, you could provide a header like this:
    Code:
    #ifndef LINKED_LIST__HH
    #define LINKED_LIST__HH
    
    typedef struct linked_list *LINKED_LIST;
    
    void add_head(LINKED_LIST, int);
    void add_tail(LINKED_LIST, int);
    void display_linked_list(LINKED_LIST);
    void free_linked_list(LINKED_LIST);
    LINKED_LIST create_linked_list(void);
    
    #endif
    Then in your source file, you would have:
    Code:
    #include "linked_list.h"
    #include "node.h"
    
    struct linked_list {
        NODE_PTR head;
        NODE_PTR tail;
    };
    
    void add_head(LINKED_LIST list, int value)
    {
        // ...
    }
    
    // ... etc ...
    In this case I chose to make LINKED_LIST be an alias for struct linked_list* because from the library user's perspective, the fact that it is a pointer is irrelevant. However, instead of NODE_PTR, I would suggest using the pointer notation directly: why say NODE_PTR when you can just say NODE* and it is therefore certain at a glance that it is a pointer?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    984
    Another approach is to identify your "private" members with some special naming and never use them outside the functions which deals with the structure directly... Example:
    Code:
    struct list {
      struct node *head_, *tail_;
    };
    Here head_ and tail_ are pointers suitable to be used just inside list_*() functions, not outside them...

    You're not 'hiding' anything, but using an 'hidding' approach to your naming convention.

  4. #4
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    64
    Quote Originally Posted by laserlight View Post
    The typical approach is to provide an opaque pointer. But this means that all the functions that make up the interface of your library must either accept a pointer parameter or return a pointer that is never dereferenced by the caller.

    So for example, you could provide a header like this:
    Code:
    #ifndef LINKED_LIST__HH
    #define LINKED_LIST__HH
    
    typedef struct linked_list *LINKED_LIST;
    
    void add_head(LINKED_LIST, int);
    void add_tail(LINKED_LIST, int);
    void display_linked_list(LINKED_LIST);
    void free_linked_list(LINKED_LIST);
    LINKED_LIST create_linked_list(void);
    
    #endif
    Then in your source file, you would have:
    Code:
    #include "linked_list.h"
    #include "node.h"
    
    struct linked_list {
        NODE_PTR head;
        NODE_PTR tail;
    };
    
    void add_head(LINKED_LIST list, int value)
    {
        // ...
    }
    
    // ... etc ...
    Just a follow up question.. We can defined a pointer to a structure(without defining the structure in the header file) because the compiler knows how big a pointer is right?

  5. #5
    Registered User
    Join Date
    Apr 2021
    Posts
    92
    Exactly correct. (And a big hint about how everything in C works, under the hood: if the compiler knows how to allocate storage for it, you can probably get it to leave you alone. If the compiler doesn't know how to allocate storage for it, you have to do something different.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-26-2018, 06:04 AM
  2. Accessing structure members
    By mc088 in forum C Programming
    Replies: 1
    Last Post: 01-26-2017, 10:42 PM
  3. How to find number of structure members in a given structure?
    By bhaskarReddy in forum C Programming
    Replies: 4
    Last Post: 01-16-2012, 05:37 AM
  4. Accessing Structure Members
    By anndruu12 in forum C Programming
    Replies: 5
    Last Post: 12-02-2010, 02:37 AM
  5. structure members outside main
    By threahdead in forum C Programming
    Replies: 5
    Last Post: 03-09-2003, 07:34 AM

Tags for this Thread