Structure in a C Program

By: Jesse Miller

A structure in a C program can contain one, or more, variables of the same, or different data types. These are grouped together in a single structure and can conveniently reference using its name. Grouping related variables together in a structure is helpful in organizing complicated data, especially in larger programs. For instance, a structure might be created to describe a payroll record, with variables to store an employee`s name, address, salary, national Insurance number, etc.

Where arrays can be used to store items of data of the same type, structures can store items of data of various data types. Each variable in an array is called an `element`, but each variable in a structure is called a `member`. Individual elements of an array are accessed by their index number; individual members of a structure are accessed by their given name.

The `struct` keyword is used to declare a structure in a C program and is followed by an optional `tag` name for that structure. All the variable members of the structure are then listed within a pair of braces. These can include arrays and pointers. A struct containing members to describe the x, y coordinates values of a point in a graph could be declared.

Each struct member can be addressed by appending the `. ` dot operator and the member`s name to the variable name. For instance, the members in the struct declaration above can be addresses as point's x, y.

Also, a new struct can be declared using the tag name of an existing struct. This new struct inherits the original member properties. For instance, the following declaration creates a struct named `top`, based on the struct named `coords`, with variables top.x and top.y.

A data type defined by a struct can be declared to be a type definition by adding the typedef keyword at the beginning of the struct declaration. This identifies that struct to be a prototype from which other structs can be created simply using any of its declared variables names, without struct keyword.

Quick Note: Taking the Nonsense out of looking for the right spyware remover

If you really want to take the work out of looking for that right Spyware Protection from a go to the Internet and get a or a Free
Download, In order to prevent your vital information from being ripped from your computer get your Remover Today.

A struct may be nested inside another struct. Struct declarations can optionally initialize all their variables as a comma-seperated list within braces. The struct can only assign a list of values to all its variables using this technique in its declaration and not later in the program.

Typically a struct will be declared in a program before the main ().

Software
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 

» More on Software
 



Share this article :
Click to see more related articles