Declaring, Filling and Reading a C Struct, with a Linux Kernel Example
The article explains how to use a C struct by first declaring it with the struct keyword, a tag, and a list of fields inside braces. It shows two ways to create a variable of that type: supplying all values at creation inside braces, or initializing to zero and assigning each field separately, with strcpy used for the array-based name field. Field values are read back using the dot operator, such as s1.marks. The author notes common pitfalls, including forgetting the semicolon after the declaration and the need to use strcpy for character arrays. The piece closes with a full example program and a brief look at a small time-tracking struct used in the Linux kernel.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in