Initialization & Accessing Structure Members in C

C programming language treats a structure as a user-defined data type; therefore you can initialize a structure like a variable. Here is an example of initialize product structure:

struct product
{
char name[50];
double price;
}
book = {
“C programming language”,
40.5
};

In above example, we defined product structure, then we declare and initialize book structure variable with its name and price.

To access structure members we can use dot operator (.) between structure variable and structure member name as follows:

<Structure-Variable-Name>.<MemberAddress>

For example to access street name of structure address we do as follows:

struct address myAddress;
myAddress.street_name = “Srinagar”;

One of major advantage of structure is you can copy it with = operator. The syntax as follows

struct_var1 = struct_var2;

However, it should be noted that both variables should be of same structure type.


For any query or issue, feel free to discuss on http://discuss.eduguru.in