gpt4 book ai didi

c++ - 使用标签名称创建结构变量

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:01:53 26 4
gpt4 key购买 nike

我已经阅读了以下链接中的一篇文章

http://www.embedded.com/electronics-blogs/programming-pointers/4024450/Tag-vs-Type-Names

这里作者说,follwing的用法是错误的。

struct s
{
--
};

s var;

但在我的示例代码中,它工作得很好。

  1 #include<iostream>
2 using namespace std;
3
4 struct s
5 {
6 int sd;
7 };
8 s v;
9
10
11
12 int main()
13 {
14
15 v.sd=10;
16 cout<<v.sd;
17 return 0;
18 }

编辑:

实际区别是什么?为什么它在 C++ 中有效而在 C 中无效;

最佳答案

这是 C++ 和 C 之间的区别。您引用的作者在谈论 C,而您使用 C++ 代码而不是 C 代码。在 C 中,必须在声明相应类型的变量之前指定关键字 struct、union 或 enum。

关于c++ - 使用标签名称创建结构变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20952533/

26 4 0