Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
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?

over 4 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;
over 4 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 Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!