C Programming language

Accessing Structure Member
Previous Home Next

Accessing structure member

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


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

1. struct address billing_addr; 2. billing_addr.country = "US";

If the structure contains another structure, we can use dot operator to access nested structure and use dot operator again to access variables of nested structure.

1. struct customer jack; 2. jack.billing_addr.country = "US"; 

Initializing structure

C programming langauge treats a structure as a custom data type therefore you can initialize a structure like a variable.

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

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

Structure and pointer

A structure can contain pointers as structure members and we can create a pointer to a structure as follows:

struct invoice{
char* code;
char date[20];
};struct address billing_addr; struct address *pa = &billing_addr;
Previous Home Next