I came across this code:
#include<stdio.h>
#include<string.h>
struct gospel
{
int num;
char mess1[50];
char mess2[50];
} m1 = { 2, "If you are driven by success",
"make sure that it is a quality drive"
};
int main()
{
struct gospel m2, m3;
m2 = m1;
m3 = m2;
printf("\n%d %s %s", m1.num, m2.mess1, m3.mess2);
return 0;
}
However I couldn't get what the line m1 =
{ 2, "If you are driven by success",
"make sure that it is a quality drive"
};
means? Can someone please explain its meaning and also the output of this program with justification.
struct gospel
{
int num;
char mess1[50];
char mess2[50];
} m1 = { 2, "If you are driven by success",
"make sure that it is a quality drive" };
Is defining a global variable that is of type struct gospel
It is the same as writing
struct gospel
{
int num;
char mess1[50];
char mess2[50];
};
struct gospel m1 = { 2, "If you are driven by success",
"make sure that it is a quality drive" };
The curly brackets assign the values of the struct to the variable by order - so m1.num is assigned the value 2, m1.mess1 is assigned the value "If you are driven by success" and m1.mess2 is assigned the value "make sure that it is a quality drive"
Would recommend reading this article if you want to learn more about this type of initialization.