c - 需要解释C编程中的结构

标签 c pointers structure nodes

struct  hello
{
  char id[5];
  int marks;
} a;

1) 'a' 是什么意思?请用简单的例子解释一下,我的讲师不擅长解释。

2)如果a是*a怎么办?我知道它是一个指针,但是我们什么时候使用它?

3)结构体节点是保留字吗?另外为什么我看到一些结构有这个“struct node *x”,我知道它是一个临时存储,但为什么它在单词节点旁边而不是在结构内部?就像这样:

struct node
{
  *x
}

4)如果结构内部有结构怎么办?我们什么时候使用它?我可以举个例子吗?

非常感谢!

最佳答案

astruct hello 的普通变量类型。

你将永远看到类似的东西

struct node
{
  *x
}

在工作程序中。可能类似于struct node *x;不过。

关于结构内的字段,它们可以像任何其他变量一样声明,因此结构内的嵌套结构可以正常工作。一个愚蠢的例子:

struct foo
{
    int a;
};

struct bar
{
    struct foo f;

    /* If you're declaring a variable or (in this case) a structure
     * member, then you don't need to give a name to the structure
     */
    struct
    {
        int a;
    } g;
};

bar结构现在有两个字段:fg 。其中每一个都是另一个包含名为 a 的成员的结构。 .

可以像这样使用

struct bar bar;  /* Structure names and variable names can be the same */
bar.f.a = 5;
bar.g.a = 10;

关于c - 需要解释C编程中的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17944760/

相关文章:

c - 在 64 位平台上取消引用整数指针

c++ - 指向 std::vector 的指针,指针声明

c - 使用指针替换字符串中的字符

c - 嵌套结构的动态数组成员

c - 虽然循环不适用于 C 编程

c - X509_STORE_add_lookup() 中的段错误

c - 在 C 中使用 pow() 函数处理整数

c - 宏 __FILE__ 与 gradle 的奇怪行为

c - C中的结构继承

C 矩阵结构