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

标签 c

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

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;
}

最佳答案

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

*response = request;

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

相关文章:

c++ - 为什么 UDP 套接字关闭不会导致选择返回?

objective-c - 除以小于 1 的 float 时无穷大?

C:尝试释放从函数返回的 malloc'd char 数组导致错误

c++ - 代码中变量旁边的 % 是什么意思?

c - 为什么需要将 NULL 转换为此宏中的类型?

c++ - 复合 if 语句使用 ? : operator in C

c - Linux pthread_suspend

c - 在 C 中获取 "void value not ignored as it ought to be"

c++ - Windows API,__declspec(thread) vs CreateThread?

c - 使用多个文件指针试图在 C 中一次打开选择的文件..?