passing a pointer to a pointer in c function by value

Keeto

fid_table is a pointer to an array of fid_list pointers.

I am trying to initialize the fid_table to NULLs in a separate function. My understanding that fid_table is copied by value but it is a pointer so that should not be the problem.

fid_list **fid_table;
fid_table_init(fid_table);
assert(fid_table[0] == NULL);

The function fid_table_init is defined as follows:

void fid_table_init(fid_list **fid_table){
    fid_table = (fid_list **) malloc(HTABLE_SIZE * sizeof(fid_list *));
    for(int i = 0; i < HTABLE_SIZE; i++){
        fid_table[i] = NULL;
    }
}

Can someone elaborate on why this assertion fails?

Vlad from Moscow

Define the function the following way

void fid_table_init(fid_list ***fid_table){
    *fid_table = (fid_list **) malloc(HTABLE_SIZE * sizeof(fid_list *));
    for(int i = 0; i < HTABLE_SIZE; i++){
        ( *fid_table )[i] = NULL;
    }
}

And call it like

fid_table_init( &fid_table );

In the original function the pointer is passed by value that is the function deals with a copy of the pointer. So any changes of the copy do not influence on the original pointer.

Take into account that function's parameters are its local variables. So inside the function you allocated a memory and assigned its address to a local variable of the function. It is the local variable that was assigned not the original pointer. After exiting the function its local variables will be destroyed. The original pointer knows nothing what was done with its copy

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C - Passing pointer to function

From Dev

Passing single pointer and double pointer to a function in c

From Dev

passing function pointer in c with a pointer argument

From Dev

C++ passing function pointer

From Dev

Passing Function Pointer in C#

From Dev

passing a pointer through a function c

From Dev

Passing a pointer (string) to a C function

From Dev

Passing char pointer to another function: blank value. c++

From Dev

changing the value of pointer to a pointer in a c function

From Dev

Boost C++. Passing a member function pointer with signature pointer to function

From Dev

Passing dereferenced pointer (ie. by value) to a function expecting a pointer

From Dev

Passing a pointer to a C++ function in a C Project

From Dev

passing a pointer address vs pointer to a pointer in C

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

Passing swift struct pointer to C function

From Dev

C++ passing in pointer to a function with reference parameters

From Dev

passing function pointer to the C code using cgo

From Dev

c struct passing itself as an argument into a pointer function

From Dev

Passing Array of Structs to a Function as a Pointer (C)

From Dev

Passing pointer to function that accepts reference in c++

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

C++ crashing when passing pointer to function

From Dev

Passing in an element in an array as a pointer in a function in C

From Dev

c function pointer return value

From Dev

Passing and Assigning New Value to Pointer C++

From Dev

Passing a pointer to an array to a function

From Dev

Passing an argument to function pointer

From Dev

Passing Function Pointer

From Dev

Passing function pointer in Dlang

Related Related

  1. 1

    C - Passing pointer to function

  2. 2

    Passing single pointer and double pointer to a function in c

  3. 3

    passing function pointer in c with a pointer argument

  4. 4

    C++ passing function pointer

  5. 5

    Passing Function Pointer in C#

  6. 6

    passing a pointer through a function c

  7. 7

    Passing a pointer (string) to a C function

  8. 8

    Passing char pointer to another function: blank value. c++

  9. 9

    changing the value of pointer to a pointer in a c function

  10. 10

    Boost C++. Passing a member function pointer with signature pointer to function

  11. 11

    Passing dereferenced pointer (ie. by value) to a function expecting a pointer

  12. 12

    Passing a pointer to a C++ function in a C Project

  13. 13

    passing a pointer address vs pointer to a pointer in C

  14. 14

    Passing a pointer to a char array as an argument to a function - C

  15. 15

    Passing swift struct pointer to C function

  16. 16

    C++ passing in pointer to a function with reference parameters

  17. 17

    passing function pointer to the C code using cgo

  18. 18

    c struct passing itself as an argument into a pointer function

  19. 19

    Passing Array of Structs to a Function as a Pointer (C)

  20. 20

    Passing pointer to function that accepts reference in c++

  21. 21

    Passing a pointer to a char array as an argument to a function - C

  22. 22

    C++ crashing when passing pointer to function

  23. 23

    Passing in an element in an array as a pointer in a function in C

  24. 24

    c function pointer return value

  25. 25

    Passing and Assigning New Value to Pointer C++

  26. 26

    Passing a pointer to an array to a function

  27. 27

    Passing an argument to function pointer

  28. 28

    Passing Function Pointer

  29. 29

    Passing function pointer in Dlang

HotTag

Archive