How to hide some fields of struct in C?How do you set, clear, and toggle a single bit?Why isn't sizeof for a struct equal to the sum of sizeof of each member?Why should we typedef a struct so often in C?Difference between 'struct' and 'typedef struct' in C++?How do function pointers in C work?typedefs of structs not seeming to go through in header files?typedef struct vs struct definitionsC typedef struct redefinition, different basic typesC struct, function pointer, and header problems (unsure which is the cause)Why does compiler see a mismatch between char * and printf's conversion specifier “s” when the char * is typedef'd and accessed through a struct?

How much character growth crosses the line into breaking the character

Is there a way to get `mathscr' with lower case letters in pdfLaTeX?

What exactly color does ozone gas have?

What should you do if you miss a job interview (deliberately)?

Can a Canadian Travel to the USA twice, less than 180 days each time?

What is Cash Advance APR?

How can mimic phobia be cured?

Mimic lecturing on blackboard, facing audience

System.QueryException unexpected token

Why should universal income be universal?

How do I delete all blank lines in a buffer?

Is this toilet slogan correct usage of the English language?

How to cover method return statement in Apex Class?

What is the evidence for the "tyranny of the majority problem" in a direct democracy context?

Why can Carol Danvers change her suit colours in the first place?

Temporarily disable WLAN internet access for children, but allow it for adults

Strong empirical falsification of quantum mechanics based on vacuum energy density?

Can a college of swords bard use blade flourish on an OA from dissonant whispers?

How should I respond when I lied about my education and the company finds out through background check?

Yosemite Fire Rings - What to Expect?

Why Shazam when there is already Superman?

Add big quotation marks inside my colorbox

Terse Method to Swap Lowest for Highest?

Is there a RAID 0 Equivalent for RAM?



How to hide some fields of struct in C?


How do you set, clear, and toggle a single bit?Why isn't sizeof for a struct equal to the sum of sizeof of each member?Why should we typedef a struct so often in C?Difference between 'struct' and 'typedef struct' in C++?How do function pointers in C work?typedefs of structs not seeming to go through in header files?typedef struct vs struct definitionsC typedef struct redefinition, different basic typesC struct, function pointer, and header problems (unsure which is the cause)Why does compiler see a mismatch between char * and printf's conversion specifier “s” when the char * is typedef'd and accessed through a struct?













7















I'm trying to implement a struct person and I need to hide some fields or make them constant.
A trick for create private fields.



Header:



#pragma once

#define NAME_MAX_LEN 20

typedef struct _person
float wage;
int groupid;
Person;

const char const *getName (Person *p);
int getId (Person *p);

/// OTHER FUNCTIONS


Source



#include "person.h"


struct _person

int id;

float wage;
int groupid;

char name[NAME_MAX_LEN];
;

/// FUNCTIONS


GCC says that person.c:7:8: error: redefinition a 'struct _person' struct _person



I can write this in a header, but after it, I can't use fields of a struct.



typedef struct _person Person;









share|improve this question









New contributor




Wootiae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 5





    C doesn't let you selectively hide fields. There's no private here.

    – user2357112
    2 hours ago











  • @user2357112 How to protect from edit my variables (id and name)?

    – Wootiae
    2 hours ago















7















I'm trying to implement a struct person and I need to hide some fields or make them constant.
A trick for create private fields.



Header:



#pragma once

#define NAME_MAX_LEN 20

typedef struct _person
float wage;
int groupid;
Person;

const char const *getName (Person *p);
int getId (Person *p);

/// OTHER FUNCTIONS


Source



#include "person.h"


struct _person

int id;

float wage;
int groupid;

char name[NAME_MAX_LEN];
;

/// FUNCTIONS


GCC says that person.c:7:8: error: redefinition a 'struct _person' struct _person



I can write this in a header, but after it, I can't use fields of a struct.



typedef struct _person Person;









share|improve this question









New contributor




Wootiae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 5





    C doesn't let you selectively hide fields. There's no private here.

    – user2357112
    2 hours ago











  • @user2357112 How to protect from edit my variables (id and name)?

    – Wootiae
    2 hours ago













7












7








7








I'm trying to implement a struct person and I need to hide some fields or make them constant.
A trick for create private fields.



Header:



#pragma once

#define NAME_MAX_LEN 20

typedef struct _person
float wage;
int groupid;
Person;

const char const *getName (Person *p);
int getId (Person *p);

/// OTHER FUNCTIONS


Source



#include "person.h"


struct _person

int id;

float wage;
int groupid;

char name[NAME_MAX_LEN];
;

/// FUNCTIONS


GCC says that person.c:7:8: error: redefinition a 'struct _person' struct _person



I can write this in a header, but after it, I can't use fields of a struct.



typedef struct _person Person;









share|improve this question









New contributor




Wootiae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I'm trying to implement a struct person and I need to hide some fields or make them constant.
A trick for create private fields.



Header:



#pragma once

#define NAME_MAX_LEN 20

typedef struct _person
float wage;
int groupid;
Person;

const char const *getName (Person *p);
int getId (Person *p);

/// OTHER FUNCTIONS


Source



#include "person.h"


struct _person

int id;

float wage;
int groupid;

char name[NAME_MAX_LEN];
;

/// FUNCTIONS


GCC says that person.c:7:8: error: redefinition a 'struct _person' struct _person



I can write this in a header, but after it, I can't use fields of a struct.



typedef struct _person Person;






c typedef






share|improve this question









New contributor




Wootiae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Wootiae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago









dbush

103k13108144




103k13108144






New contributor




Wootiae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









WootiaeWootiae

362




362




New contributor




Wootiae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Wootiae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Wootiae is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 5





    C doesn't let you selectively hide fields. There's no private here.

    – user2357112
    2 hours ago











  • @user2357112 How to protect from edit my variables (id and name)?

    – Wootiae
    2 hours ago












  • 5





    C doesn't let you selectively hide fields. There's no private here.

    – user2357112
    2 hours ago











  • @user2357112 How to protect from edit my variables (id and name)?

    – Wootiae
    2 hours ago







5




5





C doesn't let you selectively hide fields. There's no private here.

– user2357112
2 hours ago





C doesn't let you selectively hide fields. There's no private here.

– user2357112
2 hours ago













@user2357112 How to protect from edit my variables (id and name)?

– Wootiae
2 hours ago





@user2357112 How to protect from edit my variables (id and name)?

– Wootiae
2 hours ago












4 Answers
4






active

oldest

votes


















8














A struct cannot have multiple conflicting definitions. As such, you can't create a struct that hides some of the fields.



What you can do however it declare that the struct exists in the header without defining it. Then the caller is restricted to using only a pointer to the struct and using functions in your implementation to modify it.



For example, you could define your header as follows:



typedef struct _person Person;

Person *init(const char *name, int id, float wage, int groupid);

const char *getName (const Person *p);
int getId (const Person *p);
float getWage (const Person *p);
int getGroupid (const Person *p);


And your implementation would contain:



#include "person.h"

struct _person

int id;

float wage;
int groupid;

char name[NAME_MAX_LEN];
;

Person *init(const char *name, int id, float wage, int groupid)

Person *p = malloc(sizeof *p);
strcpy(p->name, name);
p->id = id;
p->wage= wage;
p->groupid= groupid;
return p;


...





share|improve this answer

























  • I would add const pointers: int getId (const Person *p); so functions can be called with constant pointers (since they're just getters)

    – Jean-François Fabre
    2 hours ago






  • 1





    @Jean-FrançoisFabre Good idea. Updated. Also, congrats on the diamond!

    – dbush
    2 hours ago











  • Can I "show" wage and groupid? for use p->wage?

    – Wootiae
    1 hour ago











  • @Wootiae Not in the calling code, because it doesn't know what Person contains. Your implementation needs an accessor function to allow the user to read it.

    – dbush
    1 hour ago











  • I know you're just copying OP's function signatures, but in const char const *getName the second const is useless and can (should) be dropped. Every return type is a rvalue and can't be modified anyway.

    – Filippo Costa
    1 hour ago


















3














C has no mechanism for hiding individual members of a structure type. However, by operating only in terms of pointers to such a type, and not providing a definition, you can make the whole type opaque. Users would then have to use the functions you provide to manipulate instances in any way. This is a thing that is sometimes done.



To some extent, you may be able to achieve something like what you describe with a hidden context. For example, consider this:



header.h



typedef struct _person 
float wage;
int groupid;
Person;


implementation.c



struct _person_real 
Person person; // must be first, and is a structure, not a pointer.
int id;
char name[NAME_MAX_LEN];
;


Now you can do this:



Person *create_person(char name[]) 
struct _person_real *pr = malloc(sizeof(*pr));

if (pr)
pr->person.wage = DEFAULT_WAGE;
pr->person.groupid = DEFAULT_GROUPID;
pr->id = generate_id();
strncpy(pr->name, name, sizeof(pr->name));
pr->name[sizeof(pr->name) - 1] = '';

return &pr->person; // <-- NOTE WELL
else
return NULL;




A pointer to the first member of a structure always points also to the whole structure, too, so if the client passes a pointer obtained from that function back to you, you can



struct _person_real *pr = (struct _person_real *) Person_pointer;


and work on the members from the larger context.



Be well aware, however, that such a scheme is risky. Nothing prevents a user from creating a Person without the larger context, and passing a pointer to it to a function that expects the context object to be present. There are other issues.



Overall, C APIs generally either take the opaque structure approach or just carefully document what clients are permitted to do with the data they have access to, or even just document how everything works, so that users can make their own choices. These, especially the latter, are well aligned with overall C approaches and idioms -- C does not hold your hand, or protect you from doing harm. It trusts you to know what you're doing, and to do only what you intend to do.






share|improve this answer























  • just document how everything works, so that users can make their own choices. The problem with that is you become locked into a specific implementation of your structure - which can only be a bad thing. If you miss something in your implementation, or your implementation precludes some new functionality you didn't think of when you designed it, you likely can only make a change if you're willing to break user's code.

    – Andrew Henle
    21 mins ago


















1














You can use a mixin style; e.g. write in the header:



struct person 
float wage;
int groupid;
;

struct person *person_new(void);
char const *getName (struct person const *p);
int getId (struct person const *p);


and in the source



struct person_impl 
struct person p;
char name[NAME_MAX_LEN];
int id;


struct person *person_new(void)

struct person_impl *p;

p = malloc(sizeof *p);
...
return &p->p;


chra const *getName(struct person const *p_)

struct person_impl *p =
container_of(p_, struct person_impl, p);

return p->name;



See e.g. https://en.wikipedia.org/wiki/Offsetof for details of container_of().






share|improve this answer






























    0














    What John Bollinger wrote is a neat way of utilising how structs and memory works, but it's also an easy way to get a segfault (imagine allocating an array of Person and then later passing the last element to a 'method' which accesses the id or it's name), or corrupt your data (in an array of Person the next Person is overwriting 'private' variables of the previous Person). You'd have to remember that you must create an array of pointers to Person instead of array of Person (sounds pretty obvious until you decide to optimise something and think that you can allocate and implement the struct more efficiently than the initialiser).



    Don't get me wrong, it's a great way to solve the problem, but you've got to be careful when using it.
    What I'd suggest (though using 4/8 bytes more memory per Person) is to create a struct Person which has a pointer to another struct which is only defined in the .c file and holds the private data. That way it'd be harder to make a mistake somewhere (and if it's a bigger project then trust me - you'll do it sooner or later).



    .h file:



    #pragma once

    #define NAME_MAX_LEN 20

    typedef struct _person
    float wage;
    int groupid;

    __personPriv *const priv;
    Person;

    void personInit(Person *p, const char *name);
    Person* personNew(const char *name);

    const char const *getName (Person *p);
    int getId (Person *p);


    .c file:



    typedef struct 
    int id;
    char name[NAME_MAX_LEN];
    __personPriv;

    const char const *getName (Person *p)
    return p->priv->name;


    int getId (Person *p)
    return p->priv->id;


    __personPriv* __personPrivNew(const char *name)
    __personPriv *ret = memcpy(
    malloc(sizeof(*ret->priv)),
    &(__personPriv)
    .id = generateId();
    ,
    sizeof(*ret->priv)
    );

    // if(strlen(name) >= NAME_MAX_LEN)
    // raise an error or something?
    // return NULL;
    //

    strncpy(ret->name, name, strlen(name));

    return ret;


    void personInit(Person *p, const char *name)
    if(p == NULL)
    return;

    p->priv = memcpy(
    malloc(sizeof(*p->priv)),
    &(__personPriv)
    .id = generateId();
    ,
    sizeof(*p->priv)
    );

    ret->priv = __personPrivNew(name);
    if(ret->priv == NULL)
    // raise an error or something



    Person* personNew(const char *name)
    Person *ret = malloc(sizeof(*ret));

    ret->priv = __personPrivNew(name);
    if(ret->priv == NULL)
    free(ret);
    return NULL;

    return ret;



    Side note: this version can be implemented so that private block is allocated right after/before the 'public' part of the struct to improve locality. Just allocate sizeof(Person) + sizeof(__personPriv) and initialise one part as Person and second one as __personPriv.






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      Wootiae is a new contributor. Be nice, and check out our Code of Conduct.









      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55307489%2fhow-to-hide-some-fields-of-struct-in-c%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      8














      A struct cannot have multiple conflicting definitions. As such, you can't create a struct that hides some of the fields.



      What you can do however it declare that the struct exists in the header without defining it. Then the caller is restricted to using only a pointer to the struct and using functions in your implementation to modify it.



      For example, you could define your header as follows:



      typedef struct _person Person;

      Person *init(const char *name, int id, float wage, int groupid);

      const char *getName (const Person *p);
      int getId (const Person *p);
      float getWage (const Person *p);
      int getGroupid (const Person *p);


      And your implementation would contain:



      #include "person.h"

      struct _person

      int id;

      float wage;
      int groupid;

      char name[NAME_MAX_LEN];
      ;

      Person *init(const char *name, int id, float wage, int groupid)

      Person *p = malloc(sizeof *p);
      strcpy(p->name, name);
      p->id = id;
      p->wage= wage;
      p->groupid= groupid;
      return p;


      ...





      share|improve this answer

























      • I would add const pointers: int getId (const Person *p); so functions can be called with constant pointers (since they're just getters)

        – Jean-François Fabre
        2 hours ago






      • 1





        @Jean-FrançoisFabre Good idea. Updated. Also, congrats on the diamond!

        – dbush
        2 hours ago











      • Can I "show" wage and groupid? for use p->wage?

        – Wootiae
        1 hour ago











      • @Wootiae Not in the calling code, because it doesn't know what Person contains. Your implementation needs an accessor function to allow the user to read it.

        – dbush
        1 hour ago











      • I know you're just copying OP's function signatures, but in const char const *getName the second const is useless and can (should) be dropped. Every return type is a rvalue and can't be modified anyway.

        – Filippo Costa
        1 hour ago















      8














      A struct cannot have multiple conflicting definitions. As such, you can't create a struct that hides some of the fields.



      What you can do however it declare that the struct exists in the header without defining it. Then the caller is restricted to using only a pointer to the struct and using functions in your implementation to modify it.



      For example, you could define your header as follows:



      typedef struct _person Person;

      Person *init(const char *name, int id, float wage, int groupid);

      const char *getName (const Person *p);
      int getId (const Person *p);
      float getWage (const Person *p);
      int getGroupid (const Person *p);


      And your implementation would contain:



      #include "person.h"

      struct _person

      int id;

      float wage;
      int groupid;

      char name[NAME_MAX_LEN];
      ;

      Person *init(const char *name, int id, float wage, int groupid)

      Person *p = malloc(sizeof *p);
      strcpy(p->name, name);
      p->id = id;
      p->wage= wage;
      p->groupid= groupid;
      return p;


      ...





      share|improve this answer

























      • I would add const pointers: int getId (const Person *p); so functions can be called with constant pointers (since they're just getters)

        – Jean-François Fabre
        2 hours ago






      • 1





        @Jean-FrançoisFabre Good idea. Updated. Also, congrats on the diamond!

        – dbush
        2 hours ago











      • Can I "show" wage and groupid? for use p->wage?

        – Wootiae
        1 hour ago











      • @Wootiae Not in the calling code, because it doesn't know what Person contains. Your implementation needs an accessor function to allow the user to read it.

        – dbush
        1 hour ago











      • I know you're just copying OP's function signatures, but in const char const *getName the second const is useless and can (should) be dropped. Every return type is a rvalue and can't be modified anyway.

        – Filippo Costa
        1 hour ago













      8












      8








      8







      A struct cannot have multiple conflicting definitions. As such, you can't create a struct that hides some of the fields.



      What you can do however it declare that the struct exists in the header without defining it. Then the caller is restricted to using only a pointer to the struct and using functions in your implementation to modify it.



      For example, you could define your header as follows:



      typedef struct _person Person;

      Person *init(const char *name, int id, float wage, int groupid);

      const char *getName (const Person *p);
      int getId (const Person *p);
      float getWage (const Person *p);
      int getGroupid (const Person *p);


      And your implementation would contain:



      #include "person.h"

      struct _person

      int id;

      float wage;
      int groupid;

      char name[NAME_MAX_LEN];
      ;

      Person *init(const char *name, int id, float wage, int groupid)

      Person *p = malloc(sizeof *p);
      strcpy(p->name, name);
      p->id = id;
      p->wage= wage;
      p->groupid= groupid;
      return p;


      ...





      share|improve this answer















      A struct cannot have multiple conflicting definitions. As such, you can't create a struct that hides some of the fields.



      What you can do however it declare that the struct exists in the header without defining it. Then the caller is restricted to using only a pointer to the struct and using functions in your implementation to modify it.



      For example, you could define your header as follows:



      typedef struct _person Person;

      Person *init(const char *name, int id, float wage, int groupid);

      const char *getName (const Person *p);
      int getId (const Person *p);
      float getWage (const Person *p);
      int getGroupid (const Person *p);


      And your implementation would contain:



      #include "person.h"

      struct _person

      int id;

      float wage;
      int groupid;

      char name[NAME_MAX_LEN];
      ;

      Person *init(const char *name, int id, float wage, int groupid)

      Person *p = malloc(sizeof *p);
      strcpy(p->name, name);
      p->id = id;
      p->wage= wage;
      p->groupid= groupid;
      return p;


      ...






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 1 hour ago

























      answered 2 hours ago









      dbushdbush

      103k13108144




      103k13108144












      • I would add const pointers: int getId (const Person *p); so functions can be called with constant pointers (since they're just getters)

        – Jean-François Fabre
        2 hours ago






      • 1





        @Jean-FrançoisFabre Good idea. Updated. Also, congrats on the diamond!

        – dbush
        2 hours ago











      • Can I "show" wage and groupid? for use p->wage?

        – Wootiae
        1 hour ago











      • @Wootiae Not in the calling code, because it doesn't know what Person contains. Your implementation needs an accessor function to allow the user to read it.

        – dbush
        1 hour ago











      • I know you're just copying OP's function signatures, but in const char const *getName the second const is useless and can (should) be dropped. Every return type is a rvalue and can't be modified anyway.

        – Filippo Costa
        1 hour ago

















      • I would add const pointers: int getId (const Person *p); so functions can be called with constant pointers (since they're just getters)

        – Jean-François Fabre
        2 hours ago






      • 1





        @Jean-FrançoisFabre Good idea. Updated. Also, congrats on the diamond!

        – dbush
        2 hours ago











      • Can I "show" wage and groupid? for use p->wage?

        – Wootiae
        1 hour ago











      • @Wootiae Not in the calling code, because it doesn't know what Person contains. Your implementation needs an accessor function to allow the user to read it.

        – dbush
        1 hour ago











      • I know you're just copying OP's function signatures, but in const char const *getName the second const is useless and can (should) be dropped. Every return type is a rvalue and can't be modified anyway.

        – Filippo Costa
        1 hour ago
















      I would add const pointers: int getId (const Person *p); so functions can be called with constant pointers (since they're just getters)

      – Jean-François Fabre
      2 hours ago





      I would add const pointers: int getId (const Person *p); so functions can be called with constant pointers (since they're just getters)

      – Jean-François Fabre
      2 hours ago




      1




      1





      @Jean-FrançoisFabre Good idea. Updated. Also, congrats on the diamond!

      – dbush
      2 hours ago





      @Jean-FrançoisFabre Good idea. Updated. Also, congrats on the diamond!

      – dbush
      2 hours ago













      Can I "show" wage and groupid? for use p->wage?

      – Wootiae
      1 hour ago





      Can I "show" wage and groupid? for use p->wage?

      – Wootiae
      1 hour ago













      @Wootiae Not in the calling code, because it doesn't know what Person contains. Your implementation needs an accessor function to allow the user to read it.

      – dbush
      1 hour ago





      @Wootiae Not in the calling code, because it doesn't know what Person contains. Your implementation needs an accessor function to allow the user to read it.

      – dbush
      1 hour ago













      I know you're just copying OP's function signatures, but in const char const *getName the second const is useless and can (should) be dropped. Every return type is a rvalue and can't be modified anyway.

      – Filippo Costa
      1 hour ago





      I know you're just copying OP's function signatures, but in const char const *getName the second const is useless and can (should) be dropped. Every return type is a rvalue and can't be modified anyway.

      – Filippo Costa
      1 hour ago













      3














      C has no mechanism for hiding individual members of a structure type. However, by operating only in terms of pointers to such a type, and not providing a definition, you can make the whole type opaque. Users would then have to use the functions you provide to manipulate instances in any way. This is a thing that is sometimes done.



      To some extent, you may be able to achieve something like what you describe with a hidden context. For example, consider this:



      header.h



      typedef struct _person 
      float wage;
      int groupid;
      Person;


      implementation.c



      struct _person_real 
      Person person; // must be first, and is a structure, not a pointer.
      int id;
      char name[NAME_MAX_LEN];
      ;


      Now you can do this:



      Person *create_person(char name[]) 
      struct _person_real *pr = malloc(sizeof(*pr));

      if (pr)
      pr->person.wage = DEFAULT_WAGE;
      pr->person.groupid = DEFAULT_GROUPID;
      pr->id = generate_id();
      strncpy(pr->name, name, sizeof(pr->name));
      pr->name[sizeof(pr->name) - 1] = '';

      return &pr->person; // <-- NOTE WELL
      else
      return NULL;




      A pointer to the first member of a structure always points also to the whole structure, too, so if the client passes a pointer obtained from that function back to you, you can



      struct _person_real *pr = (struct _person_real *) Person_pointer;


      and work on the members from the larger context.



      Be well aware, however, that such a scheme is risky. Nothing prevents a user from creating a Person without the larger context, and passing a pointer to it to a function that expects the context object to be present. There are other issues.



      Overall, C APIs generally either take the opaque structure approach or just carefully document what clients are permitted to do with the data they have access to, or even just document how everything works, so that users can make their own choices. These, especially the latter, are well aligned with overall C approaches and idioms -- C does not hold your hand, or protect you from doing harm. It trusts you to know what you're doing, and to do only what you intend to do.






      share|improve this answer























      • just document how everything works, so that users can make their own choices. The problem with that is you become locked into a specific implementation of your structure - which can only be a bad thing. If you miss something in your implementation, or your implementation precludes some new functionality you didn't think of when you designed it, you likely can only make a change if you're willing to break user's code.

        – Andrew Henle
        21 mins ago















      3














      C has no mechanism for hiding individual members of a structure type. However, by operating only in terms of pointers to such a type, and not providing a definition, you can make the whole type opaque. Users would then have to use the functions you provide to manipulate instances in any way. This is a thing that is sometimes done.



      To some extent, you may be able to achieve something like what you describe with a hidden context. For example, consider this:



      header.h



      typedef struct _person 
      float wage;
      int groupid;
      Person;


      implementation.c



      struct _person_real 
      Person person; // must be first, and is a structure, not a pointer.
      int id;
      char name[NAME_MAX_LEN];
      ;


      Now you can do this:



      Person *create_person(char name[]) 
      struct _person_real *pr = malloc(sizeof(*pr));

      if (pr)
      pr->person.wage = DEFAULT_WAGE;
      pr->person.groupid = DEFAULT_GROUPID;
      pr->id = generate_id();
      strncpy(pr->name, name, sizeof(pr->name));
      pr->name[sizeof(pr->name) - 1] = '';

      return &pr->person; // <-- NOTE WELL
      else
      return NULL;




      A pointer to the first member of a structure always points also to the whole structure, too, so if the client passes a pointer obtained from that function back to you, you can



      struct _person_real *pr = (struct _person_real *) Person_pointer;


      and work on the members from the larger context.



      Be well aware, however, that such a scheme is risky. Nothing prevents a user from creating a Person without the larger context, and passing a pointer to it to a function that expects the context object to be present. There are other issues.



      Overall, C APIs generally either take the opaque structure approach or just carefully document what clients are permitted to do with the data they have access to, or even just document how everything works, so that users can make their own choices. These, especially the latter, are well aligned with overall C approaches and idioms -- C does not hold your hand, or protect you from doing harm. It trusts you to know what you're doing, and to do only what you intend to do.






      share|improve this answer























      • just document how everything works, so that users can make their own choices. The problem with that is you become locked into a specific implementation of your structure - which can only be a bad thing. If you miss something in your implementation, or your implementation precludes some new functionality you didn't think of when you designed it, you likely can only make a change if you're willing to break user's code.

        – Andrew Henle
        21 mins ago













      3












      3








      3







      C has no mechanism for hiding individual members of a structure type. However, by operating only in terms of pointers to such a type, and not providing a definition, you can make the whole type opaque. Users would then have to use the functions you provide to manipulate instances in any way. This is a thing that is sometimes done.



      To some extent, you may be able to achieve something like what you describe with a hidden context. For example, consider this:



      header.h



      typedef struct _person 
      float wage;
      int groupid;
      Person;


      implementation.c



      struct _person_real 
      Person person; // must be first, and is a structure, not a pointer.
      int id;
      char name[NAME_MAX_LEN];
      ;


      Now you can do this:



      Person *create_person(char name[]) 
      struct _person_real *pr = malloc(sizeof(*pr));

      if (pr)
      pr->person.wage = DEFAULT_WAGE;
      pr->person.groupid = DEFAULT_GROUPID;
      pr->id = generate_id();
      strncpy(pr->name, name, sizeof(pr->name));
      pr->name[sizeof(pr->name) - 1] = '';

      return &pr->person; // <-- NOTE WELL
      else
      return NULL;




      A pointer to the first member of a structure always points also to the whole structure, too, so if the client passes a pointer obtained from that function back to you, you can



      struct _person_real *pr = (struct _person_real *) Person_pointer;


      and work on the members from the larger context.



      Be well aware, however, that such a scheme is risky. Nothing prevents a user from creating a Person without the larger context, and passing a pointer to it to a function that expects the context object to be present. There are other issues.



      Overall, C APIs generally either take the opaque structure approach or just carefully document what clients are permitted to do with the data they have access to, or even just document how everything works, so that users can make their own choices. These, especially the latter, are well aligned with overall C approaches and idioms -- C does not hold your hand, or protect you from doing harm. It trusts you to know what you're doing, and to do only what you intend to do.






      share|improve this answer













      C has no mechanism for hiding individual members of a structure type. However, by operating only in terms of pointers to such a type, and not providing a definition, you can make the whole type opaque. Users would then have to use the functions you provide to manipulate instances in any way. This is a thing that is sometimes done.



      To some extent, you may be able to achieve something like what you describe with a hidden context. For example, consider this:



      header.h



      typedef struct _person 
      float wage;
      int groupid;
      Person;


      implementation.c



      struct _person_real 
      Person person; // must be first, and is a structure, not a pointer.
      int id;
      char name[NAME_MAX_LEN];
      ;


      Now you can do this:



      Person *create_person(char name[]) 
      struct _person_real *pr = malloc(sizeof(*pr));

      if (pr)
      pr->person.wage = DEFAULT_WAGE;
      pr->person.groupid = DEFAULT_GROUPID;
      pr->id = generate_id();
      strncpy(pr->name, name, sizeof(pr->name));
      pr->name[sizeof(pr->name) - 1] = '';

      return &pr->person; // <-- NOTE WELL
      else
      return NULL;




      A pointer to the first member of a structure always points also to the whole structure, too, so if the client passes a pointer obtained from that function back to you, you can



      struct _person_real *pr = (struct _person_real *) Person_pointer;


      and work on the members from the larger context.



      Be well aware, however, that such a scheme is risky. Nothing prevents a user from creating a Person without the larger context, and passing a pointer to it to a function that expects the context object to be present. There are other issues.



      Overall, C APIs generally either take the opaque structure approach or just carefully document what clients are permitted to do with the data they have access to, or even just document how everything works, so that users can make their own choices. These, especially the latter, are well aligned with overall C approaches and idioms -- C does not hold your hand, or protect you from doing harm. It trusts you to know what you're doing, and to do only what you intend to do.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 1 hour ago









      John BollingerJohn Bollinger

      84.2k74279




      84.2k74279












      • just document how everything works, so that users can make their own choices. The problem with that is you become locked into a specific implementation of your structure - which can only be a bad thing. If you miss something in your implementation, or your implementation precludes some new functionality you didn't think of when you designed it, you likely can only make a change if you're willing to break user's code.

        – Andrew Henle
        21 mins ago

















      • just document how everything works, so that users can make their own choices. The problem with that is you become locked into a specific implementation of your structure - which can only be a bad thing. If you miss something in your implementation, or your implementation precludes some new functionality you didn't think of when you designed it, you likely can only make a change if you're willing to break user's code.

        – Andrew Henle
        21 mins ago
















      just document how everything works, so that users can make their own choices. The problem with that is you become locked into a specific implementation of your structure - which can only be a bad thing. If you miss something in your implementation, or your implementation precludes some new functionality you didn't think of when you designed it, you likely can only make a change if you're willing to break user's code.

      – Andrew Henle
      21 mins ago





      just document how everything works, so that users can make their own choices. The problem with that is you become locked into a specific implementation of your structure - which can only be a bad thing. If you miss something in your implementation, or your implementation precludes some new functionality you didn't think of when you designed it, you likely can only make a change if you're willing to break user's code.

      – Andrew Henle
      21 mins ago











      1














      You can use a mixin style; e.g. write in the header:



      struct person 
      float wage;
      int groupid;
      ;

      struct person *person_new(void);
      char const *getName (struct person const *p);
      int getId (struct person const *p);


      and in the source



      struct person_impl 
      struct person p;
      char name[NAME_MAX_LEN];
      int id;


      struct person *person_new(void)

      struct person_impl *p;

      p = malloc(sizeof *p);
      ...
      return &p->p;


      chra const *getName(struct person const *p_)

      struct person_impl *p =
      container_of(p_, struct person_impl, p);

      return p->name;



      See e.g. https://en.wikipedia.org/wiki/Offsetof for details of container_of().






      share|improve this answer



























        1














        You can use a mixin style; e.g. write in the header:



        struct person 
        float wage;
        int groupid;
        ;

        struct person *person_new(void);
        char const *getName (struct person const *p);
        int getId (struct person const *p);


        and in the source



        struct person_impl 
        struct person p;
        char name[NAME_MAX_LEN];
        int id;


        struct person *person_new(void)

        struct person_impl *p;

        p = malloc(sizeof *p);
        ...
        return &p->p;


        chra const *getName(struct person const *p_)

        struct person_impl *p =
        container_of(p_, struct person_impl, p);

        return p->name;



        See e.g. https://en.wikipedia.org/wiki/Offsetof for details of container_of().






        share|improve this answer

























          1












          1








          1







          You can use a mixin style; e.g. write in the header:



          struct person 
          float wage;
          int groupid;
          ;

          struct person *person_new(void);
          char const *getName (struct person const *p);
          int getId (struct person const *p);


          and in the source



          struct person_impl 
          struct person p;
          char name[NAME_MAX_LEN];
          int id;


          struct person *person_new(void)

          struct person_impl *p;

          p = malloc(sizeof *p);
          ...
          return &p->p;


          chra const *getName(struct person const *p_)

          struct person_impl *p =
          container_of(p_, struct person_impl, p);

          return p->name;



          See e.g. https://en.wikipedia.org/wiki/Offsetof for details of container_of().






          share|improve this answer













          You can use a mixin style; e.g. write in the header:



          struct person 
          float wage;
          int groupid;
          ;

          struct person *person_new(void);
          char const *getName (struct person const *p);
          int getId (struct person const *p);


          and in the source



          struct person_impl 
          struct person p;
          char name[NAME_MAX_LEN];
          int id;


          struct person *person_new(void)

          struct person_impl *p;

          p = malloc(sizeof *p);
          ...
          return &p->p;


          chra const *getName(struct person const *p_)

          struct person_impl *p =
          container_of(p_, struct person_impl, p);

          return p->name;



          See e.g. https://en.wikipedia.org/wiki/Offsetof for details of container_of().







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          enscensc

          4,456815




          4,456815





















              0














              What John Bollinger wrote is a neat way of utilising how structs and memory works, but it's also an easy way to get a segfault (imagine allocating an array of Person and then later passing the last element to a 'method' which accesses the id or it's name), or corrupt your data (in an array of Person the next Person is overwriting 'private' variables of the previous Person). You'd have to remember that you must create an array of pointers to Person instead of array of Person (sounds pretty obvious until you decide to optimise something and think that you can allocate and implement the struct more efficiently than the initialiser).



              Don't get me wrong, it's a great way to solve the problem, but you've got to be careful when using it.
              What I'd suggest (though using 4/8 bytes more memory per Person) is to create a struct Person which has a pointer to another struct which is only defined in the .c file and holds the private data. That way it'd be harder to make a mistake somewhere (and if it's a bigger project then trust me - you'll do it sooner or later).



              .h file:



              #pragma once

              #define NAME_MAX_LEN 20

              typedef struct _person
              float wage;
              int groupid;

              __personPriv *const priv;
              Person;

              void personInit(Person *p, const char *name);
              Person* personNew(const char *name);

              const char const *getName (Person *p);
              int getId (Person *p);


              .c file:



              typedef struct 
              int id;
              char name[NAME_MAX_LEN];
              __personPriv;

              const char const *getName (Person *p)
              return p->priv->name;


              int getId (Person *p)
              return p->priv->id;


              __personPriv* __personPrivNew(const char *name)
              __personPriv *ret = memcpy(
              malloc(sizeof(*ret->priv)),
              &(__personPriv)
              .id = generateId();
              ,
              sizeof(*ret->priv)
              );

              // if(strlen(name) >= NAME_MAX_LEN)
              // raise an error or something?
              // return NULL;
              //

              strncpy(ret->name, name, strlen(name));

              return ret;


              void personInit(Person *p, const char *name)
              if(p == NULL)
              return;

              p->priv = memcpy(
              malloc(sizeof(*p->priv)),
              &(__personPriv)
              .id = generateId();
              ,
              sizeof(*p->priv)
              );

              ret->priv = __personPrivNew(name);
              if(ret->priv == NULL)
              // raise an error or something



              Person* personNew(const char *name)
              Person *ret = malloc(sizeof(*ret));

              ret->priv = __personPrivNew(name);
              if(ret->priv == NULL)
              free(ret);
              return NULL;

              return ret;



              Side note: this version can be implemented so that private block is allocated right after/before the 'public' part of the struct to improve locality. Just allocate sizeof(Person) + sizeof(__personPriv) and initialise one part as Person and second one as __personPriv.






              share|improve this answer



























                0














                What John Bollinger wrote is a neat way of utilising how structs and memory works, but it's also an easy way to get a segfault (imagine allocating an array of Person and then later passing the last element to a 'method' which accesses the id or it's name), or corrupt your data (in an array of Person the next Person is overwriting 'private' variables of the previous Person). You'd have to remember that you must create an array of pointers to Person instead of array of Person (sounds pretty obvious until you decide to optimise something and think that you can allocate and implement the struct more efficiently than the initialiser).



                Don't get me wrong, it's a great way to solve the problem, but you've got to be careful when using it.
                What I'd suggest (though using 4/8 bytes more memory per Person) is to create a struct Person which has a pointer to another struct which is only defined in the .c file and holds the private data. That way it'd be harder to make a mistake somewhere (and if it's a bigger project then trust me - you'll do it sooner or later).



                .h file:



                #pragma once

                #define NAME_MAX_LEN 20

                typedef struct _person
                float wage;
                int groupid;

                __personPriv *const priv;
                Person;

                void personInit(Person *p, const char *name);
                Person* personNew(const char *name);

                const char const *getName (Person *p);
                int getId (Person *p);


                .c file:



                typedef struct 
                int id;
                char name[NAME_MAX_LEN];
                __personPriv;

                const char const *getName (Person *p)
                return p->priv->name;


                int getId (Person *p)
                return p->priv->id;


                __personPriv* __personPrivNew(const char *name)
                __personPriv *ret = memcpy(
                malloc(sizeof(*ret->priv)),
                &(__personPriv)
                .id = generateId();
                ,
                sizeof(*ret->priv)
                );

                // if(strlen(name) >= NAME_MAX_LEN)
                // raise an error or something?
                // return NULL;
                //

                strncpy(ret->name, name, strlen(name));

                return ret;


                void personInit(Person *p, const char *name)
                if(p == NULL)
                return;

                p->priv = memcpy(
                malloc(sizeof(*p->priv)),
                &(__personPriv)
                .id = generateId();
                ,
                sizeof(*p->priv)
                );

                ret->priv = __personPrivNew(name);
                if(ret->priv == NULL)
                // raise an error or something



                Person* personNew(const char *name)
                Person *ret = malloc(sizeof(*ret));

                ret->priv = __personPrivNew(name);
                if(ret->priv == NULL)
                free(ret);
                return NULL;

                return ret;



                Side note: this version can be implemented so that private block is allocated right after/before the 'public' part of the struct to improve locality. Just allocate sizeof(Person) + sizeof(__personPriv) and initialise one part as Person and second one as __personPriv.






                share|improve this answer

























                  0












                  0








                  0







                  What John Bollinger wrote is a neat way of utilising how structs and memory works, but it's also an easy way to get a segfault (imagine allocating an array of Person and then later passing the last element to a 'method' which accesses the id or it's name), or corrupt your data (in an array of Person the next Person is overwriting 'private' variables of the previous Person). You'd have to remember that you must create an array of pointers to Person instead of array of Person (sounds pretty obvious until you decide to optimise something and think that you can allocate and implement the struct more efficiently than the initialiser).



                  Don't get me wrong, it's a great way to solve the problem, but you've got to be careful when using it.
                  What I'd suggest (though using 4/8 bytes more memory per Person) is to create a struct Person which has a pointer to another struct which is only defined in the .c file and holds the private data. That way it'd be harder to make a mistake somewhere (and if it's a bigger project then trust me - you'll do it sooner or later).



                  .h file:



                  #pragma once

                  #define NAME_MAX_LEN 20

                  typedef struct _person
                  float wage;
                  int groupid;

                  __personPriv *const priv;
                  Person;

                  void personInit(Person *p, const char *name);
                  Person* personNew(const char *name);

                  const char const *getName (Person *p);
                  int getId (Person *p);


                  .c file:



                  typedef struct 
                  int id;
                  char name[NAME_MAX_LEN];
                  __personPriv;

                  const char const *getName (Person *p)
                  return p->priv->name;


                  int getId (Person *p)
                  return p->priv->id;


                  __personPriv* __personPrivNew(const char *name)
                  __personPriv *ret = memcpy(
                  malloc(sizeof(*ret->priv)),
                  &(__personPriv)
                  .id = generateId();
                  ,
                  sizeof(*ret->priv)
                  );

                  // if(strlen(name) >= NAME_MAX_LEN)
                  // raise an error or something?
                  // return NULL;
                  //

                  strncpy(ret->name, name, strlen(name));

                  return ret;


                  void personInit(Person *p, const char *name)
                  if(p == NULL)
                  return;

                  p->priv = memcpy(
                  malloc(sizeof(*p->priv)),
                  &(__personPriv)
                  .id = generateId();
                  ,
                  sizeof(*p->priv)
                  );

                  ret->priv = __personPrivNew(name);
                  if(ret->priv == NULL)
                  // raise an error or something



                  Person* personNew(const char *name)
                  Person *ret = malloc(sizeof(*ret));

                  ret->priv = __personPrivNew(name);
                  if(ret->priv == NULL)
                  free(ret);
                  return NULL;

                  return ret;



                  Side note: this version can be implemented so that private block is allocated right after/before the 'public' part of the struct to improve locality. Just allocate sizeof(Person) + sizeof(__personPriv) and initialise one part as Person and second one as __personPriv.






                  share|improve this answer













                  What John Bollinger wrote is a neat way of utilising how structs and memory works, but it's also an easy way to get a segfault (imagine allocating an array of Person and then later passing the last element to a 'method' which accesses the id or it's name), or corrupt your data (in an array of Person the next Person is overwriting 'private' variables of the previous Person). You'd have to remember that you must create an array of pointers to Person instead of array of Person (sounds pretty obvious until you decide to optimise something and think that you can allocate and implement the struct more efficiently than the initialiser).



                  Don't get me wrong, it's a great way to solve the problem, but you've got to be careful when using it.
                  What I'd suggest (though using 4/8 bytes more memory per Person) is to create a struct Person which has a pointer to another struct which is only defined in the .c file and holds the private data. That way it'd be harder to make a mistake somewhere (and if it's a bigger project then trust me - you'll do it sooner or later).



                  .h file:



                  #pragma once

                  #define NAME_MAX_LEN 20

                  typedef struct _person
                  float wage;
                  int groupid;

                  __personPriv *const priv;
                  Person;

                  void personInit(Person *p, const char *name);
                  Person* personNew(const char *name);

                  const char const *getName (Person *p);
                  int getId (Person *p);


                  .c file:



                  typedef struct 
                  int id;
                  char name[NAME_MAX_LEN];
                  __personPriv;

                  const char const *getName (Person *p)
                  return p->priv->name;


                  int getId (Person *p)
                  return p->priv->id;


                  __personPriv* __personPrivNew(const char *name)
                  __personPriv *ret = memcpy(
                  malloc(sizeof(*ret->priv)),
                  &(__personPriv)
                  .id = generateId();
                  ,
                  sizeof(*ret->priv)
                  );

                  // if(strlen(name) >= NAME_MAX_LEN)
                  // raise an error or something?
                  // return NULL;
                  //

                  strncpy(ret->name, name, strlen(name));

                  return ret;


                  void personInit(Person *p, const char *name)
                  if(p == NULL)
                  return;

                  p->priv = memcpy(
                  malloc(sizeof(*p->priv)),
                  &(__personPriv)
                  .id = generateId();
                  ,
                  sizeof(*p->priv)
                  );

                  ret->priv = __personPrivNew(name);
                  if(ret->priv == NULL)
                  // raise an error or something



                  Person* personNew(const char *name)
                  Person *ret = malloc(sizeof(*ret));

                  ret->priv = __personPrivNew(name);
                  if(ret->priv == NULL)
                  free(ret);
                  return NULL;

                  return ret;



                  Side note: this version can be implemented so that private block is allocated right after/before the 'public' part of the struct to improve locality. Just allocate sizeof(Person) + sizeof(__personPriv) and initialise one part as Person and second one as __personPriv.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 29 mins ago









                  GrabuszGrabusz

                  4116




                  4116




















                      Wootiae is a new contributor. Be nice, and check out our Code of Conduct.









                      draft saved

                      draft discarded


















                      Wootiae is a new contributor. Be nice, and check out our Code of Conduct.












                      Wootiae is a new contributor. Be nice, and check out our Code of Conduct.











                      Wootiae is a new contributor. Be nice, and check out our Code of Conduct.














                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55307489%2fhow-to-hide-some-fields-of-struct-in-c%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Are there any AGPL-style licences that require source code modifications to be public? Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Force derivative works to be publicAre there any GPL like licenses for Apple App Store?Do you violate the GPL if you provide source code that cannot be compiled?GPL - is it distribution to use libraries in an appliance loaned to customers?Distributing App for free which uses GPL'ed codeModifications of server software under GPL, with web/CLI interfaceDoes using an AGPLv3-licensed library prevent me from dual-licensing my own source code?Can I publish only select code under GPLv3 from a private project?Is there published precedent regarding the scope of covered work that uses AGPL software?If MIT licensed code links to GPL licensed code what should be the license of the resulting binary program?If I use a public API endpoint that has its source code licensed under AGPL in my app, do I need to disclose my source?

                      2013 GY136 Descoberta | Órbita | Referências Menu de navegação«List Of Centaurs and Scattered-Disk Objects»«List of Known Trans-Neptunian Objects»

                      Button changing it's text & action. Good or terrible? The 2019 Stack Overflow Developer Survey Results Are Inchanging text on user mouseoverShould certain functions be “hard to find” for powerusers to discover?Custom liking function - do I need user login?Using different checkbox style for different checkbox behaviorBest Practices: Save and Exit in Software UIInteraction with remote validated formMore efficient UI to progress the user through a complicated process?Designing a popup notice for a gameShould bulk-editing functions be hidden until a table row is selected, or is there a better solution?Is it bad practice to disable (replace) the context menu?