gpt4 book ai didi

c - 在 C 中将 const 赋值给非常量

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

在以下内容中:

struct adt { void * A; };

int new_adt(const void * const A)
{
struct adt * r = malloc(sizeof(struct adt));
r->A = A;
}

我得到:

warning: assignment discards qualifiers from pointer target type


我知道我可以用

memcpy(&(r->A), &A, sizeof(void *));

要解决它,但我必须问:还有其他选择吗?

通过使用 const void * const 我假装说不会对输入进行任何更改。另外,现在我想到了, const void * 就足够了,不是吗? (因为我不能改变指针以影响调用者)

感谢您花时间阅读。

最佳答案

By using const void * const I pretend to say that no changes will be made to the input.

没有假装说不会对输入进行任何更改,您已明确告诉编译器您有< strong>保证不会对输入进行任何更改。

绝不能做你正在做的事。

关于c - 在 C 中将 const 赋值给非常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4963990/

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