Bifalgorithm post · Computer science one · Nested Structures

How should structure names and member variables be declared in C?

How should structure names and member variables be declared in C? (Computer science one · Nested Structures) — "typedef struct Student { 2 ... 3 } Student; Whi…

Ideas in this flowchart

  • C structure naming and internal member-variable conventions
  • The typedef declaration places the Student identifier in both the global space and the structure space.
  • The declaration style depends on several factors; for simplicity, the first style is used here.
  • Older conventions may use lowercase underscore names for structures.
  • POSIX systems use names ending in _t to indicate a structure type.
  • Such naming conventions should be avoided to prevent conflicts.
  • C does not provide a mechanism for protecting data.
  • Libraries and code bases may begin member variables with underscores to mark them as internal.
  • _someVariable and __anotherVariable can indicate variables that should not be accessed or modified.
  • C still allows these variables to be accessed and modified.
  • Tampering may violate library assumptions and expectations, leading to undefined behavior.
Feed/

How should structure names and member variables be declared in C?

No cover

Channel

Computer science one

1 page

"typedef struct Student { 2 ... 3 } Student; Which places the Student identifier in both the global space and in the “structure” space. Which style of declaration you use depends on several factors, but for simplicity we’ll stick with the first style. In addition, you may see some older naming conventions use lowercase underscore naming for structure names. In particular, POSIX systems use names that end with _t (indicating a structure type). You should avoid such naming conventions to avoid conflicts. Finally, though C does not provide a mechanism for the protection of data, many libraries and code bases will begin certain member variables with underscore(s) to indicate that they are “internal” variables used by the library and should not be accessed or modified. For example, _someVariable or __anotherVariable would indicate “private” variables that should not be tampered with. The C language itself would still allow you access and modify the variables, but doing so may violate assumptions and expectations of the library code, leading to undefined behavior."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.