简体   繁体   中英

Please explain this struct usage

#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 ];

I don't understand the second struct, can you please explain what it does? How does the constant change the struct if we change the constant?

Update

I think it's the same as:

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

The following

struct thread_info thread_info_array[ MAX_THREADS ];

is an array of the previously declared thread_info structs. The array consists of MAX_THREADS elements; if you change the constant, the size of the array will change.

See the C FAQ for why the second struct keyword is required.

struct thread_info thread_info_array[ MAX_THREADS ]; implies thread_info_array is an array of thread_info structures of MAX_THREADS elements.

Changing the constant only changes the number of elements in the array but will not impact the struct definition.

It's not "second struct".

This:

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

is a type definition .

This:

struct thread_info thread_info_array[ MAX_THREADS ];

is an array definition of MAX_THREADS elements where each element is of type struct thread_info that you've defined above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM