• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

80
Views
Adding const-ness to opaque handle

If I have created a C module that presents a handle to the user with a pointer to a forward declared struct, like so:

typedef struct FOO_Obj *FOO_Handle;

If I then declare function prototypes that use it as a const qualified parameter thusly:

void FOO_work(const FOO_Handle fooHandle);

How is the const-ness applied?

const struct FOO_Obj *FOO_Handle // A
struct FOO_Obj *const FOO_Handle  // B
const struct FOO_Obj *const FOO_Handle  // C

Or is it UB?

about 3 years ago · Santiago Trujillo
1 answers
Answer question

0

B. ( There is no undefined behavior with the code you presented. )

The function call

void FOO_work(const FOO_Handle fooHandle);

is equivalent to

void FOO_work(struct FOO_Obj* const fooHandle);

Variable fooHandle in the function will becode a const pointer to a non-const struct FOO_Obj object. You will not be able to add the const qualifier to fooHandle to make it a pointer to a const object.

Instead, if you want to have a pointer to a const object, and keep the struct hidden, you must make another typedef:

typedef const struct FOO_Obj* FOO_ConstHandle;
about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error