What is struct (C structure)?

A C mechanism that groups several values (members), possibly of different types, into a single type. It is defined as in struct sensor { int id; float value; }; the members of a variable s are used as s.id, and through a pointer p to a struct as p->id. Members are laid out in memory in the order of declaration, but padding may be inserted between them for alignment, so the whole may be larger than the sum of the member sizes.

Japanese name: 構造体

struct (C structure) appears in 9 practice questions across 5 topics, including ISA & Assembly, C Programming Basics, Embedded Systems & Firmware.

Field: C Programming Basics

Definition and use

With the definition struct point { int x; int y; }; a variable is created as in struct point p = {1, 2}; and each member is used as p.x or p.y. Giving an alias with typedef, as in typedef struct { … } Point; allows the declaration Point p; without writing struct. Structs can be assigned as a whole with =, passed to functions and returned from them, but large structs are generally passed by pointer.

Layout in memory

Members are placed in the order of declaration, from lower addresses upward. Padding is inserted before a type that must be aligned, such as a 4-byte int on a 4-byte boundary, and the size of the whole struct is rounded up to a multiple of its strictest alignment. So a struct with char, int and char in that order takes 12 bytes in a typical environment with a 4-byte int, and putting the int first reduces it to 8 bytes.

Pitfalls in embedded code and communication

Representing the layout of peripheral registers with a struct is common, but padding and the bit order of bit-fields may differ between implementations, so using them without checking can shift addresses. Treating communication data or a file format as the raw memory image of a struct breaks compatibility with other environments because of differences in padding and endianness, so the data is often assembled byte by byte instead.

Try it: questions about struct (C structure)

When representing peripheral registers with C bit-field structs, what needs care?

  1. ABit fields always make programs twice as fast
  2. BBit ordering and similar details of bit fields are implementation-defined, so another compiler may manipulate unintended bits
  3. CBit fields cannot represent values of 8 bits or more
Show answer and explanation

Answer: B. Bit ordering and similar details of bit fields are implementation-defined, so another compiler may manipulate unintended bits

Bit fields read nicely, but the C standard leaves to the implementation whether bits are allocated from the top or bottom of a unit, how fields crossing boundaries are handled and the access width. Code that works with one compiler may therefore read or write different bits with another compiler or CPU. Headers supplied by microcontroller vendors are usually written with masks and shifts.

FAQ

What is the difference between a struct and an array?

An array lines up elements of the same type, selected by an index. A struct groups members of possibly different types, selected by name. The two are often combined, as in an array of structs or a struct that has an array as a member.

Why should the size of a struct be checked with sizeof?

The amount of padding between and after members depends on type sizes and alignment rules (the ABI), so adding up member sizes does not give the correct size. When allocating a struct with malloc or writing it to a file, the actual size is obtained with sizeof(struct name).

struct (C structure) in the explanations

Questions that mention struct (C structure) (9)

  1. When representing peripheral registers with C bit-field structs, what needs care?
  2. What does the RISC-V instruction "lw t0, 8(sp)" do?
  3. In the standard RISC-V calling convention, when calling a function with ten integer arguments, where are the ninth and tenth passed?
  4. In RISC-V, what can happen when the address of a 4-byte lw is not a multiple of 4 (misaligned)?
  5. Which correctly describes an ABI (application binary interface)?
  6. On Linux, when is the version number in a shared library's soname, such as the ".1" in libfoo.so.1, generally increased?
  7. False sharing occurs with an array of per-thread counters such as "long cnt[8];". What is the most suitable fix?
  8. Assume a typical environment where char is 1 byte and int is 4 bytes, with int placed at addresses that are multiples of 4. Which of the following is the output when the code above is executed?
  9. The header file sensor.h is written as above. Which of the following is the purpose of the three lines #ifndef, #define and #endif?

Books on C Programming Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Stack Calling convention Argument Pointer Alignment and padding Array Bit-field Microcontroller (MCU) Stack pointer Application binary interface (ABI)

Back to the glossary