Reputation: 2979
What do I misunderstand 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;
}
Upvotes: 5
Views: 272
Reputation: 19266
Firstly, with the currect declaration and your usage of get_response
, the parameter response
is declared as char**
, which is a pointer to a char*
, e.g. a pointer to a pointer. This would be useful if you somehow needed to modify the pointer actually pointing to the memory containing your response
, but in this case this is not needed.
Upvotes: 0
Reputation: 35039
in the function get_response
you store the address of request
in the temporary variable response
. You want to store it where response
points to.
*response = request;
Upvotes: 2
Reputation: 3405
You want *response = request;
instead of response = &request;
in get_response(...)
Upvotes: 0
Reputation: 9740
Try *response = request
: you want to set the contents of the response pointer to the request content.
Upvotes: 0