gpt4 book ai didi

c - 如何将结构指针传递给具有常量值的函数

转载 作者:太空宇宙 更新时间:2023-11-04 08:00:01 25 4
gpt4 key购买 nike

我有:

typedef struct LIST *List;

struct LIST
{
Node *head;
int numNodes;
};

如果我想将 List 的实例传递给函数并保持指向的值不变,合适的原型(prototype)应该是什么样的?

到目前为止,我已经尝试过原型(prototype)如下所示的函数:

void func1(const List list);

void func2(List const list);

两者似乎都使指针本身保持不变,但似乎都没有使指向的值保持不变。

非常感谢任何帮助:)

最佳答案

Both seem to keep the the pointer itself constant where neither seem to keep the value pointed to constant.

这正是为什么如果他们打算继续并将该类型视为指针,则不应隐藏指针语义。摆脱混淆的 typedef:

void func1(struct LIST const *p_list);

任何试图根据类型对 const 重新排序的尝试都是转移注意力。 “前导 const 具有误导性”的常见情况。

关于c - 如何将结构指针传递给具有常量值的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47369052/

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