How to point to the expected char array after passing char* into a function in C?

lkkeepmoving

Why I can not change the content the char* points to?
For example:

int main(int argc, char * argv[]) {
    char *a = NULL; // now a = NULL
    b(a);
    // but now a points to NULL again! Why?
}

void b(char *argv[], char* c) {
    // now a is passed in 
    c = *argv[3];
    // now a actually points to the start of char array pointed to by argv[3]
}

From the output, I see the *a is passed to the function. Then inside the function b, it actually pointed to the expected char[ ]. But when returned from b, a became NULL again. Why? And how can a points to expected content after return from b? Thank you!

Sergey Kalinichenko

C passes everything by value, including pointers. Function parameters are similar to local variables that acquire their initial value from the caller. Once you are inside the function, the value of the parameter can be changed freely without influencing the values passed by the caller.

C's way of passing modifiable stuff into a function is passing pointers. If you want to modify a value of some type T, you pass a pointer to T; if you want to modify a pointer to T, you must pass a pointer to a pointer to T, and so on. In other words, the level of indirection must be higher by one than that of the value being changed.

How does this apply to your case? Your function b needs to modify a pointer to char, hence it needs to take a pointer to a pointer to a char, like this:

void b(char *argv[], char **ppc) {
    ...
}

Inside the function you modify the pointer to which ppc points by dereferencing ppc once:

*ppc = argv[3]; // No asterisk here in front of `argv`

Of course calling b from the main needs to take the address of a, like this:

b(argv, &a);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to point to the expected char array after passing char* into a function in C?

From Dev

Passing char array to function

From Dev

C programming passing a char array into a function

From Dev

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

From Dev

C18 Passing a char array to function

From Dev

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

From Dev

Passing an array of an array of char to a function

From Dev

Passing an array of an array of char to a function

From Dev

Passing a char pointer array to a function

From Dev

Passing pointer of char array to function

From Dev

Passing char array to another function

From Dev

Passing const char * to a function in C

From Dev

Passing const char * to a function in C

From Dev

Passing a buffer (char*) to a function in C

From Dev

passing char array to function to get the value of update char array

From Dev

How to pass an array of char's to a function in C

From Dev

How to pass in function a multidimensional char array in C?

From Dev

How to pass in function a multidimensional char array in C?

From Dev

C: array of char pointers not working as expected dynamically

From Dev

Passing an unsigned char array by reference in C++

From Dev

Passing a char array as parameter in method, C language

From Dev

how can I make a copy the same value on char pointer(its point at) from char array in C?

From Dev

Multidimensional char array in C in function

From Dev

Multidimensional char array in C in function

From Dev

Returning char array in C function

From Dev

How to convert a char array to a char pointer in C

From Dev

How to copy char* to char* array in C

From Dev

How to add a char/int to an char array in C?

From Dev

How to convert a char array to a char pointer in C

Related Related

  1. 1

    How to point to the expected char array after passing char* into a function in C?

  2. 2

    Passing char array to function

  3. 3

    C programming passing a char array into a function

  4. 4

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

  5. 5

    C18 Passing a char array to function

  6. 6

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

  7. 7

    Passing an array of an array of char to a function

  8. 8

    Passing an array of an array of char to a function

  9. 9

    Passing a char pointer array to a function

  10. 10

    Passing pointer of char array to function

  11. 11

    Passing char array to another function

  12. 12

    Passing const char * to a function in C

  13. 13

    Passing const char * to a function in C

  14. 14

    Passing a buffer (char*) to a function in C

  15. 15

    passing char array to function to get the value of update char array

  16. 16

    How to pass an array of char's to a function in C

  17. 17

    How to pass in function a multidimensional char array in C?

  18. 18

    How to pass in function a multidimensional char array in C?

  19. 19

    C: array of char pointers not working as expected dynamically

  20. 20

    Passing an unsigned char array by reference in C++

  21. 21

    Passing a char array as parameter in method, C language

  22. 22

    how can I make a copy the same value on char pointer(its point at) from char array in C?

  23. 23

    Multidimensional char array in C in function

  24. 24

    Multidimensional char array in C in function

  25. 25

    Returning char array in C function

  26. 26

    How to convert a char array to a char pointer in C

  27. 27

    How to copy char* to char* array in C

  28. 28

    How to add a char/int to an char array in C?

  29. 29

    How to convert a char array to a char pointer in C

HotTag

Archive