【问题标题】:Please explain this struct usage请解释这个结构的用法
【发布时间】:2013-03-23 10:08:23
【问题描述】:
#define MAX_THREADS ( 17 )
struct thread_info
{
  unsigned int * thread_sp; /* Storage space for thread stack-pointer. */
  int thread_id;            /* Storage space for a thread ID. */
};
struct thread_info thread_info_array[ MAX_THREADS ];

我不明白第二个结构,你能解释一下它的作用吗?如果我们改变常量,常量如何改变结构体?

更新

我认为它与以下内容相同:

struct thread_info { unsigned int *thread_sp; int thread_id; } thread_info_array[MAX_THREADS];

【问题讨论】:

  • 不太一样,那个单一版本缺少结构标签。
  • @teppic 谢谢。 struct thread_info { unsigned into * thread_sp; int thread_id; } thread_info_array[MAX_THREADS]; (?)
  • 几乎一样。但是在这里,您定义了一个未命名类型的变量 - 您将无法在其他任何地方使用此类型,例如与第一个版本相比,您不能再定义任何此类变量。
  • @icepack - 标签存在,没有问题。
  • @teppic 同意,问题已编辑

标签: c arrays multithreading struct


【解决方案1】:

这不是“第二个结构”。

这个:

struct thread_info
{
  unsigned int * thread_sp; /* Storage space for thread stack-pointer. */
  int thread_id;            /* Storage space for a thread ID. */
};

是一个类型定义

这个:

struct thread_info thread_info_array[ MAX_THREADS ];

是 MAX_THREADS 个元素的数组定义,其中每个元素的类型为您在上面定义的 struct thread_info

【讨论】:

    【解决方案2】:

    以下

    struct thread_info thread_info_array[ MAX_THREADS ];
    

    是先前声明的 thread_info 结构的数组。该数组由MAX_THREADS 元素组成;如果改变常量,数组的大小就会改变。

    请参阅 C FAQ 了解为什么需要第二个 struct 关键字。

    【讨论】:

      【解决方案3】:

      struct thread_info thread_info_array[ MAX_THREADS ]; 暗示 thread_info_array 是由 MAX_THREADS 元素组成的 thread_info 结构数组。

      更改常量只会更改数组中元素的数量,但不会影响struct 的定义。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-23
        • 1970-01-01
        • 2013-03-19
        • 1970-01-01
        • 2013-03-31
        • 1970-01-01
        • 1970-01-01
        • 2017-09-20
        相关资源
        最近更新 更多