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 6080693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:05:27+00:00 2026-05-23T11:05:27+00:00

I have an opaque type in my library defined as: typedef struct MyOpaqueType* MyType;

  • 0

I have an opaque type in my library defined as:

typedef struct MyOpaqueType* MyType;  // easier to type for client code

I can’t pass a pointer-to-const struct around using the typedef, so some functions look like:

void UsePointerToConst ( const struct MyOpaqueType * )

instead of:

void UserPointerToConst( const MyType ) // can't use, is really constant pointer

So, given this, I have two questions:
Is the struct keyword in the parameter list only necessary in C?
Is there a better way to do this? Should I create a typedef such as:

typedef const struct MyOpaqueType* ConstantMyType; ?
  • 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-23T11:05:28+00:00Added an answer on May 23, 2026 at 11:05 am

    Is the struct keyword in the parameter list only necessary in C?

    Yes. See Jens Gustedt’s answer.

    Is there a better way to do this?

    Just typedef the struct, not the pointer. This is better because

    • you only need one typedef instead of one for each of {MyOpaqueType, MyOpaqueType *, MyOpaqueType const *, MyOpaqueType *const and MyOpaqueType const *const} and all variants involving restrict (which doesn’t exist in C++),
    • it’s clear to the user that pointer semantics apply, i.e., passing the datatype around is really a matter of pointer copying (no performance worries), users are less likely to forget cleaning up after use, C++ users may use smart pointers, and
    • it’s a common C convention (think FILE *).

    There’s also no danger; when someone forgets the *, they get a compiler error.

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

Sidebar

Related Questions

Does someone have a good explanation of what an opaque type is? I saw
I have a style defined for a JLabel: <style id=myLabel> <state> <opaque value=true/> <color
I have this opaque type type_t and a function prototype like foo(type_t *t) and
I have an OpenGL texture with transparent and opaque pixels (eg, texture contains a
I am trying to use css to have a background image with opaque attributes
I want to have a command in Emacs to make it as opaque/transparent as
I have a masking CGContext with 2 types of pixels: color and alpha (opaque
I have a corba releated question. In my Java app I use typedef sequence
The C standard guarantees that size_t is a type that can hold any array
I have such code in my view: <% @items.each do |item| %> <tr<%= raw(item.presence

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.