I need to explain how pointers work when passed as args functions

It seemed to me that I understood the basics of pointers, but after checking some documentation on some sqlite3 methods, I was thrown, so now I'm not sure that my understanding is correct.

Here is the sqlite3 method call:

char* dataFilePath = "foobar.sqlite";
if (sqlite3_open(dataFilePath, &database) != SQLITE_OK) {...}   

And here is the function header declaration:

int sqlite3_open(
  const char *filename,   /* Database filename (UTF-8) */
  sqlite3 **ppDb          /* OUT: SQLite db handle */
);

Why does the database suddenly become a pointer to a pointer?

Another method call to close the database connection: sqlite3_close (database);

In the function header:

int sqlite3_close(sqlite3 *);

Why is it just a pointer when I pass a pointer? Would it be a pointer to a pointer?

Of all the examples that I saw, it always seemed to be the reverse of the above functions, i.e.

// function
void foo(someDataType *bar) { ... }

// function call
foo(&bar);

Thanks for the help.

+5
source share
5 answers

, sqlite3_open . (sqlite3), . :

typedef struct { /*...*/ } sqlite3;

int sqlite3_open(const char *filename, sqlite3 **ppDb) {
    /* ... */

    // Allocate memory for the database handle.
    *ppDb = (sqlite3 *)malloc(sizeof(sqlite3));

    /* ... */
    return 0;
}

sqlite3_close free :

int sqlite3_close(sqlite3 *pDb) {
    /* ... Cleanup stuff ... */

    free(pDb);

    return 0;
}
+15

, , , , "&" " "

int value = 0;
int *pointer = &value;
int **doublePointer = &pointer;
+5

- .

, database sqlite3* database;, &database ( ) database.

sqlite3_open , , . sqlite . sqlite3_close , , , , .

+4

, . . , : C " " ?.

0

, sqlite. .

, .

int var1=0;
in *ptr1=&var1;
func(var1, ptr1);

var1 = 5

var1 = 0xff2200 (- )

ptr1 = 0xff2200 ( var1)

ptr1 = 0xff0022 (- )

, var arg

void func1(int x, int *p){
    x+=5;
    (*p)-=5;
}

;

func(var1, ptr1);

var1 0!!! -5

; func1

x = 0 ( var1)

x = 0xaabbcc (- , var1!!! x + = 5 var1. ! u , . " ...)

p = 0xcccccc (- )

p = 0xff2200 ( ptr1 var1! var1, )

, var. , ; . -in-, .

, , ...

And "pass by reference" means "pass by pointer", other languages ​​do not use pointers. so sometimes you have to follow the link. But in C, pointers will do the job ...

0
source

All Articles