Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1109207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:11:50+00:00 2026-05-17T02:11:50+00:00

In C++ and Java, data structures can have private , public and protected regions.

  • 0

In C++ and Java, data structures can have private, public and protected regions. I’d like to port this concept to a C language program I am writing.

Are there any idioms for implementing private or protected function pointers and data fields in a C struct?
I know that C structs are public, I’m looking for an idiom to help hide some implementation details and force users to use the public interface.

Note: The language has been chosen by the shop, so I am stuck implementing Object Oriented concepts into C.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:11:51+00:00Added an answer on May 17, 2026 at 2:11 am

    As you know, you cannot do this. However, there are idioms that will allow a similar effect.

    C will allow you do do something similar to what is known as the “pimpl” idiom in object-oriented design. Your struct can have an opaque pointer to another forward-declared struct that acts as the struct’s private data. Functions that operate on the struct, taking the place of member functions, can have the full definition for the private member, and can make use of it, while other parts of the code cannot. For example:

    In a header, foo.h:

      struct FooPrivate;
    
      struct Foo {
         /* public: */
           int x; 
           double y;
         /* private: */
           struct FooPrivate* p;
      };
    
      extern struct Foo* Foo_Create(); /* "constructor" */
    
      extern void Foo_DoWhatever(struct Foo* foo); /* "member function" */
    

    In the implementation, foo.c:

      struct FooPrivate {
         int z;
      };
    
      struct Foo* Foo_Create()
      {
         struct Foo* foo = malloc(sizeof(Foo));
    
         foo->p = malloc(sizeof(FooPrivate));
    
         foo->x = 0;
         foo->y = 0;
         foo->p->z = 0;
    
         return foo;
      }
    
      void Foo_DoWhatever(struct Foo* foo) 
      {
          foo->p->z = 4; /* Can access "private" parts of foo */
      }
    

    In a program:

      #include "foo.h"
    
      int main()
      {
          struct Foo* foo = Foo_Create();
    
          foo->x = 100; /* Can access "public" parts of foo */
          foo->p->z = 20; /* Error! FooPrivate is not fully declared here! */
    
          Foo_DoWhatever(foo); /* Can call "member" function */
    
          return 0;
      }
    

    Note the need to use a “constructor” function in order to allocate memory for the private data. Obviously you would need to pair this with a special “destructor” function in order to deallocate the private data properly.

    Or, alternatively, if you would like your struct to have no public fields whatsoever, you could make the entire struct opaque, and just have the header be something like

      struct Foo;
    
      extern struct Foo* Foo_Create(); /* "constructor" */
    
      extern void Foo_DoWhatever(struct Foo* foo); /* "member function" */
    

    With the actual definition of struct Foo in foo.c, and getter and setter functions available for any properties you would like to provide direct access to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This semester in university I have a class called Data Structures, and the professor
This is very similar to another question ( Functional Data Structures in Java )
I have implemented a java program . This is basically a multi threaded service
I have a Java data structure which results from deserialising this JSON: { 'level1
We are doing some empirical testing of Java data structures and have got some
It's for homework for my Data Structures class, and it's in Java. I have
I have the following problem in my Data Structures and Problem Solving using Java
This is strange to me: when I run in Java byte[] data = new
I have a complex Clojure data structure that I would like to serialize -
I'm trying to implement some data structures (like HAMP-hash array mapped trie) in Common

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.