Question: Explain the purpose of a struct.
Answer: I suppose you ask about “struct” key word in C/C++ programming language.
“struct” key word defines a conglomerate user data type.
We know that an array can hold a large collection of data. The problem with array's is that all elements in an array be it multi dimensional or single dimension, must be of the same data type. So, an array is an aggregate of elements of the same type. A struct is an aggregate of elements of
(nearly) arbitrary types. For example:
struct address {
char* name;
long int number;
char* street;
char* town;
char state[2];
long zip;
}This defines a new type called “address” consisting of the items you need in order to send mail to someone.
Variables of type address can be declared exactly as other variables, and the individual members can be accessed using the . (dot) operator. For example:
void f()
{
address jd;
jd.name = "Jim Danndy";
jd.number = 61;
}
Comments