Given the following struct definition:
struct A
{
int member_b;
int member_c;
};
declare x to have this structure type. Initialize the members of x member_b
and member_c, to the values 1 and 2, respectively.
Note: This requests an initialization, not an assignment of the members.
This distinction is important and will be made in the text in a later
chapter.
To declare a variable x
of the struct A
type and initialize its members member_b
and member_c
to the values 1 and 2 respectively, you can use the following C++ code is as:
PROGRAM CODE:
//Header file section
#include <iostream>
using namespace std;
//Structure class
struct A
{
int member_b;
int member_c;
};
//Program starts a main program
int main()
{
// Declare a variable 'x' of type 'struct A'.
A x;
// Initialize members 'member_b' and 'member_c' with values 1 and 2 respectively.
x.member_b = 1;
x.member_c = 2;
// Print the values of the members to verify the initialization.
cout << "x.member_b: " << x.member_b << endl;
cout << "x.member_c: " << x.member_c << endl;
return 0;
}
OUTPUT:
x.member_b: 1
x.member_c: 2