gpt4 book ai didi

c++ - 从 C 到 C++ 的常量结构中的 char 数组的静态初始化

转载 作者:IT老高 更新时间:2023-10-28 22:38:10 26 4
gpt4 key购买 nike

我有一些现有的 C 代码:

extern const struct sockaddr_un addr =
{
.sun_family = AF_UNIX,
.sun_path = "myreallylongpath"
};

其中 sun_path 是一个字符数组。

这曾经在旧版本的 GCC 中编译为 C。我现在已将其转换为 C++ 并使用 GCC v4.7.2。我不断收到错误:

"C99 designator 'sun_path' outside aggregate intializer"

难道我在 C++ 中不能做我正在做的事情,还是语法与旧 C 不同?

最佳答案

指定初始化器是在 C99 中引入的,GCC 也支持它们作为 GNU89 中的扩展,但在 C++ 中不支持。

所以你需要使用 C89 风格,C++ 也支持这种风格。由于结构只有这两个字段:

extern const struct sockaddr_un addr =
{
AF_UNIX,
"myreallylongpath"
};

引用:Designated Initializers

关于c++ - 从 C 到 C++ 的常量结构中的 char 数组的静态初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19178124/

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