Bifalgorithm post · Computer science one · Declaration & Initialization

How are structure instances declared and initialized?

How are structure instances declared and initialized? (Computer science one · Declaration & Initialization) — "Once we have defined a structure (and included t…

Ideas in this flowchart

  • Déclaration et initialisation d’instances de structures en C
  • La structure doit être définie et son fichier d’en-tête inclus avant la création d’instances.
  • Les déclarations statiques peuvent créer des instances comme « Student s; » et « Student t; ».
  • Ces déclarations réservent sur la pile l’espace nécessaire aux membres de Student et de Date.
  • Sans initialisation explicite, les valeurs des membres sont indéfinies, car C ne fournit pas de valeurs par défaut.
  • Une autre syntaxe permet d’initialiser les instances avec des accolades.
  • Une liste vide, comme « Student s = {}; », attribue zéro aux types numériques et null aux pointeurs.
  • Des valeurs entre accolades initialisent les membres dans l’ordre de leur déclaration.
  • Comme dateOfBirth est une structure imbriquée, ses valeurs nécessitent un autre ensemble d’accolades.
  • Les chaînes « Grace » et « Hopper » sont placées dans une mémoire en lecture seule ; les modifier est indéfini, mais les pointeurs peuvent être réaffectés.
  • Les structures occupant plusieurs éléments, leur allocation sur la pile peut provoquer un dépassement de pile si elles deviennent trop grandes.
Feed/

How are structure instances declared and initialized?

No cover

Channel

Computer science one

1 page

"Once we have defined a structure (and included the header file it has been defined in), we can create instances using the usual syntax.\n\nStudent s;\n\nStudent t;\n\nThese static declarations will allocate enough space on the stack to hold all of the data associated with the structures (the two char * pointers, int, and double and the three int variables in the Date structure). However, the values stored in each of the structure’s member variables are undefined. With this style of declaration, C does not define default values.\n\nAnother way to declare instances of our structures is to use the following syntax.\n\nStudent s = {};\n\nStudent t = {\n\n12345678,\n\n\"Grace\",\n\n\"Hopper\",\n\n4.0,\n\n{\n\n1906,\n\n1,\n\n1\n\n}\n\n};\n\nThe first declaration creates a Student structure with default values (zero for any numeric types, null for any pointers). The second creates a Student structure initialized with the values provided in the curly brackets. The order matters here and will match the ordering of the original structure declaration. Since the dateOfBirth is a structure itself, a nested set of values within curly brackets is necessary. One draw back to this type of declaration is that the character pointers are statically declared. The strings \"Grace\" and \"Hopper\" are initialized in a read-only segment of memory, any attempts to change the contents of these strings are undefined behavior. The pointers themselves, however, can be reassigned.\n\nThis static declaration allocates the structure on the stack. Since structures consist of multiple pieces of data, their memory footprint is larger. Allocating larger and larger structures on the stack runs the risk of running out of stack memory resulting in a stack overflow."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.