Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Never defined structure

Tags:

c

struct

typedef

Is there any benefit in having never-defined structures in C ?

Example in SQLite source code :

/* struct sqlite3_stmt is never defined */
typedef struct sqlite3_stmt sqlite3_stmt;

And the object is manipulated like so :

typedef struct Vdbe Vdbe;
struct Vdbe {
    /* lots of members */
};


int sqlite3_step(sqlite3_stmt *pStmt) {
    Vdbe *v = (Vdbe*) pStmt;
    /* do stuff with v... */
}

So why not just use a usual abstract type, with the actual structure privately defined in foo.c source and a public typedef in foo.h header ?

like image 387
Axel Avatar asked Jul 19 '10 09:07

Axel


People also ask

How a structure can be defined?

A structure can be defined as a single entity holding variables of different data types that are logically related to each other. All the data members inside a structure are accessible to the functions defined outside the structure.

Can you define a structure without a name?

Anonymous unions/structures are also known as unnamed unions/structures as they don't have names. Since there is no names, direct objects(or variables) of them are not created and we use them in nested structure or unions.

What can a structure not contain?

The book says, "A structure cannot contain an instance of itself. For example, a variable of type struct employee cannot be declared in the definition for struct employee .

Can a structure be empty?

In this article we are going to learn about size of structure with no members (or Empty Structure) in C language with an example. Yes, it is allowed in C programming language that we can declare a structure without any member and in that case the size of the structure with no members will be 0 (Zero).


1 Answers

It is defined like this to hide the implementation detail of sqlite3_stmt from the user, thus avoiding the internal states from being messed around. See Opaque pointer.

(This also forces the user only to use the type as a pointer since the structure sqlite3_stmt itself has incomplete implementation.)


Edit: VDBE (virtual database engine) is just "a" back-end of the SQLite3 API. I believe the back-end is changeable, thus a sqlite3_stmt* is not necessarily a Vdbe*. Not exposing Vdbe* in the API because the back-end detail should not be exposed.

like image 109
kennytm Avatar answered Oct 25 '22 00:10

kennytm