gpt4 book ai didi

c - 函数调用期间不兼容的指针类型

转载 作者:行者123 更新时间:2023-11-30 18:35:09 25 4
gpt4 key购买 nike

我已将代码缩减为以下内容:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void change_data (char *data);

int main (int argc, char *argv[])
{
char data[20] = {0};

strcpy(data, "This is a test.");
change_data(&data);

exit(EXIT_SUCCESS);
}

void change_data (char *data)
{
printf("%s\n", data);
}

我希望能够更改函数中的主变量。我认为使用 & 意味着发送地址而不是复制数据。但编译时出现以下错误:

tt.c: In function 'main':
tt.c:13:14: warning: passing argument 1 of 'change_data' from incompatible pointer type [-Wincompatible-pointer-types]
change_data(&data);
^
tt.c:5:6: note: expected 'char *' but argument is of type 'char (*)[20]'
void change_data (char *data);
^~~~~~~~~~~

有人可以告诉我我做错了什么吗,因为我不知道。泰。

最佳答案

删除&data 的类型为 char[20],因此指向它的指针的类型为 char (*)[20]。当需要时,数组会衰减为指针类型,因此只需传递data就会将其作为char*传递。

关于c - 函数调用期间不兼容的指针类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47964978/

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