gpt4 book ai didi

c - 将指向 C 数组的指针传递给函数

转载 作者:太空狗 更新时间:2023-10-29 15:07:33 25 4
gpt4 key购买 nike

我对将指针传递给 char 数组有什么误解?

Request pointer in fun: 0x7fffde9aec80Response pointer in fun: 0x7fffde9aec80Response pointer: (nil), expected: 0x7fffde9aec80Response 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;
}

最佳答案

在函数 get_response 中,您将 request 的地址存储在临时变量 response 中。你想把它存储在response指向的地方。

*response = request;

关于c - 将指向 C 数组的指针传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9414661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com