Skip to main content

Posts

Showing posts with the label Structures and Unions

Structures and Unions

A structure is a user-defined data type available in C that allows to combining data items of different kinds. Structures are used to represent a record. Defining a structure: To define a structure, you must use the struct keyword followed by an optional struct tag followed by the body of the struct. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows: struct [structure name] { member definition; member definition; ... member definition; }; Eg: struct student { int rno; char name[50]; int mark; char sex; float height; } S; Here S is a structure variable. Structure member can be any valid data type(primitive, arrays) including other structures and pointers. A structure may not, for obvious reasons, contain instances of itself but may contain pointers to instances of itself. We can also create array of structures or pointer to a struct. Accessing the members of a structure: The