gpt4 book ai didi

C(Linux): warning: assignment discards qualifiers from pointer target type

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

我有以下代码片段,在编译时向我发出警告(代码无论如何都可以工作):

char *x_first_name, *x_last_name, *x_address, *x_zip;

const size_t firsts_count = sizeof(firsts) / sizeof(firsts[0]);
const size_t lasts_count = sizeof(lasts) / sizeof(lasts[0]);
const size_t streets_count = sizeof(streets) / sizeof(streets[0]);
const size_t zips_count = sizeof(zips) / sizeof(zips[0]);

srand(time(NULL));

x_first_name = firsts[rand() % firsts_count]; // line 69
x_last_name = lasts[rand() % lasts_count]; // line 70
x_address = streets[rand() % streets_count]; // line 71
x_zip = zips[rand() % zips_count]; // line 72

编译警告:

authorize.c: In function 'main':
authorize.c:69: warning: assignment discards qualifiers from pointer target type
authorize.c:70: warning: assignment discards qualifiers from pointer target type
authorize.c:71: warning: assignment discards qualifiers from pointer target type
authorize.c:72: warning: assignment discards qualifiers from pointer target type

第一个、最后一个、街道和邮政编码声明为:

const char *firsts[] = {
"Asgar",
"Aadit",
"Aanand",
"Aaron"
};

我做错了什么?

最佳答案

正如编译器所述,您在赋值时会丢弃指针中的限定符(在本例中为 const)。

当您为 char * 分配 const char * 的值时,就会发生这种情况。

关于C(Linux): warning: assignment discards qualifiers from pointer target type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17087845/

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