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 :
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));
}