Book
Collection
Click for Table of Contents
© 2025 by Rance D. Necaise
C Primer for Python Programmers
Copyright © 2025
Rance D. Necaise

9.2 Initializing Struct Variables

A struct variable can be initialized when it is first created. Consider the Card structure that we defined in the previous section

  1. struct Card {
  2.   char suit;
  3.   int value;
  4. };

We can create new Card variables and initialize them at the same time using the notation:

  1. struct Card king = {.suit = 'H', .value = 13};
  2. struct Card queen = {.suit = 'D', .value = 12};

Here, we have created two additional Cards and initialized them to represent the king of hearts and the queen of diamonds:

The order in which the fields are specified within curly braces does not matter and not all fields have to be specified. If a field is not specified as part of the initialization, that field will store the value that was previously in that area of memory.

Syntax
Initializing struct Variables
struct struct-type var-name = {.field = value, 
                          .field2 = value, 
                              :
                         };
The initial values of the struct fields can be initialized when the struct variable is first created. The values must be provided within a pair of curly braces ({ }) in the form (.field = value).
  • Each field is specified by name which must be preceded by a period.
  • The value, which can be a literal or a variable, is assigned to the field using the normal assignment (=) notation.
  • The value assigned to a field must be of the same data type as the field.

Copying Structs

Unlike arrays, struct variables can be copied between variables of the same type. For example, if we declare the struct variable

  1. struct Card save;

we can copy the contents of the king struct variable to the new save variable with a simple assignment statement

  1. save = king;

which produces a new Card variable initialized to be the same as the king variable

When a struct variable is assigned or copied, the entire contents of the struct is copied to the destination variable, byte by byte. Thus, it will be an identical copy.

Data Field Types

A struct type can be defined to store any type of data, including an array or even another struct. Suppose we want to define a struct to store an address that includes integers and strings:

  1. const STREET_NAME_SIZE = 35;
  2. const CITY_NAME_SIZE = 35;
  3. const STATE_ABBRV_SIZE = 3;
  4.  
  5. struct Address {
  6.   int number;
  7.   char street[STREET_NAME_SIZE];
  8.   char state[STATE_ABBRV_SIZE];
  9.   int zipcode;
  10. };

We can declare a variable of the new struct type

  1. struct Address whiteHouse = {
  2.                   .number = 1600,
  3.                   .street = "Pennsylvania Ave",    
  4.                   .state = "DC",
  5.                   .zipcode = 20500 };

which results in

Question 9.2.1

Consider the following declarations

  1. struct Date {
  2.   int month;
  3.   int day;
  4.   int year;
  5. };
  6.  
  7. Date today;

Would the following assignment be valid?

  1. today = ace;

Why or why not?

No, it would not be valid. Although we can assign one struct variable to another, the assignment must be between struct variables of the same type.

Question 9.2.2

Declare a Card variable and initialize it at the time of declaration to represent the Jack of Clubs playing card.

  1. struct Card jack = {.suit = 'C', .value = 11};
Question 9.2.3

Declare an Address variable and initialize it to store the address of the State Department: 2201 C Street NW, DC, 20500.

  1. struct Address stateDept = {.number = 2201,
  2.                             .street = "C Street NW",
  3.                             .state = "DC", .zipcode = 20500};
Question 9.2.4

Define a new structured type Date to represent a day of the year specified as "July 4, 1776".

  1. struct Date {
  2.   int day;
  3.   char month[10];
  4.   int year;
  5. };