Nearby lessons

59 of 72

C - Structure Initialisation

Structure initialisation means giving values to structure members when a variable is created. This lesson covers the basic brace method and what happens when some values are not provided.

1. Basic Structure Initialisation

Give the values inside braces when declaring the structure variable. The values follow the order in which members are defined.

Example01
Loading editor…

2. The Order of Values Matters

In a normal brace initialiser, the first value goes to the first member, the second value goes to the second member, and so on. Keep the order correct.

Example02
Loading editor…

3. Partial Initialisation

If you provide fewer values than the structure has members, the remaining members are set to zero.

Example03
Loading editor…

4. Initialise All Members to Zero

Use {0} to initialise the whole structure to zero. This is a simple and useful option when you want a clean starting value.

Example04
Loading editor…

Common Mistakes

  • Do not provide values in the wrong order.
  • Do not assume an uninitialised local structure contains zeros.
  • Use braces at the time of declaration, for example struct Point p = {10, 20};.
📝 Key Takeaways
  • Use braces to provide initial values.
  • Values in a basic initialiser follow the order of the structure members.
  • Members not provided in an initialiser are set to zero.
  • Use {0} when you want to initialise all members to zero.
  • A local structure declared without an initialiser is not automatically set to zero.

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.