Passing a pointer to a C array of a function

What I don’t understand about passing pointers to char arrays?

  Request pointer in fun: 0x7fffde9aec80
 Response pointer in fun: 0x7fffde9aec80
 Response pointer: (nil), expected: 0x7fffde9aec80
 Response itself: (null), expected: Yadda
#include <stdlib.h> #include <stdio.h> #include <string.h> int get_response(char *request, char **response) { response = &request; printf("Request pointer in fun: %p\n", request); printf("Response pointer in fun: %p\n", *response); return 0; } int main() { char *response = NULL, request[] = "Yadda"; get_response(request, &response); printf("Response pointer: %p, expected: %p\n", response, request); printf("Response itself: %s, expected: %s\n", response, request); return 0; } 
+4
source share
4 answers

in the get_response function get_response you store the request address in the response temporary variable. You want to save it where response points to.

 *response = request; 
+2
source

Try *response = request : you want to set the contents of the response pointer to the contents of the request.

0
source

Do you want *response = request; instead of response = &request; in get_response(...)

0
source

First, with a currect expression and using get_response the response parameter is declared as char** , which is a pointer to char* , for example. pointer to pointer. This would be useful if you somehow needed to change the pointer, which actually points to the memory containing your response , but in this case it is not necessary.

0
source

Source: https://habr.com/ru/post/1398045/


All Articles