Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Understanding Opaque Pointer's

I apologize if this is an obvious question, but in C, Opaque Pointers are similar to Java Interfaces? As I’m trying to understand them, I’m interpreting them similarly to java interfaces in the sense that they establish a mandatory way to interact with the method/function? So if I wanted to use an Opaque Pointer, I would need to define it in a .h file while I would call it in a .c file? Opaque pointers just define the signature of the method/function (in the .h file) while the the .c file takes those functions with those signatures and implements them

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

No. An opaque pointer is a design where the implementation of a type (usually struct; optionally typedef‘ed) is hidden (encapsulated) from the client. This implies that instances are heap allocated and clients have to use functions provided by the module(?) to operate on it.

You will have something like this in your header file:

#ifndef FOO_H
#define FOO_H

typedef struct foo foo; // forward declaration
foo *create_foo();

#endif

and then you implement this:

#include <stdlib.h>
#include "foo.h"

struct foo {
   int data;
};

foo *create_foo() {
   return calloc(1, sizeof(foo));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading