Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to use opaque structs where the fields are hidden from the user and enforce immutability in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Opaque structs prevent modification because the caller cannot see the fields

// const variable — prevents modification of a value
// pointer to const — data immutable
// const pointer — pointer immutable
// fully immutable pointer — both immutable
// opaque structs — strongest immutability
// const in APIs (functions) — enforce read‑only access

// Forward declaration (opaque type)
typedef struct User User;

// Only allow creation, read-only access, and freeing
const User* create_user(int id, const char *name);
int get_user_id(const User *u);
const char* get_user_name(const User *u);
void free_user(const User *u);  


// Internal struct definition (hidden from user)
struct User {
    int id;
    char name[16];
};

const User* create_user(int id, const char *name) {
    User *u = malloc(sizeof(User));
    if (!u) {
        return NULL; // allocation failed
    }

    u->id = id;

    // Copy name safely into fixed-size buffer
    strncpy(u->name, name, sizeof(u->name) - 1);
    u->name[sizeof(u->name) - 1] = '\0';  // ensure null-termination

    return u;  // returned as const pointer (immutable to caller)
}

int get_user_id(const User *u) {
    return u->id;
}

const char* get_user_name(const User *u) {
    return u->name;
}

void free_user(const User *u) {
    // Cast away const because we know we allocated it internally
    free((void*)u);
}


int main() {
    const User *u = create_user(42, "Tom");

    if (!u) {
        printf("Memory allocation failed\n");
        return 1;
    }

    printf("User ID: %d\n", get_user_id(u));
    printf("User Name: %s\n", get_user_name(u));

    // u->id = 123;  // ERROR: incomplete type prevents access
    // u = NULL;    // allowed: pointer itself is not const here

    free_user(u);  // free memory safely

    return 0;
}


/*
run:

User ID: 42
User Name: Tom

*/

 



answered Jun 8 by avibootz
edited Jun 8 by avibootz
...