Structures

Declaring Structure

Syntax :

struct structure-tag
{
    Member1 declaration;
    Member1 declaration;
        |
        |
  }structure name={initializer
 values};

In the above  declaration struct keyword is used to declare the structures  followed by a structure-name. structure-tag(optional) should follow the rules of naming a variable. Inside the structure body structure members(The members can be any valid variable type, including arrays, structures, and unions.) are declared and structure body ends with semicolon. 

When using the structure_name and the tag_name, you can choose any of the following: 

• If a structure_name is not specified and a tag_name is specified, the structure is being defined but not declared. 

• Suppose a structure_name is specified and a tag_name is not specified, the structure is being declared but not defined. 

• Suppose a structure_name and a tag_name are provided, the structure is being both defined and declared. 

• If neither a structure_name nor a tag_name is provided, a compile-time error will result. If you want to initialize the structure, you must have a structure_name because it signals the compiler that this is a declaration. The structure_name is also necessary if you want to refer to the structure. After the structure_name are optional initializers: {initializer_values};

Structures provides us a number of advantages, including the following: You can refer to the whole data object using a single name. You can use the structure name as a argument/parameter to a function. For example, you could pass the address and the length of the structure name to Input() function to read the structure’s contents from a disk file. Structures can be assigned directly. You cannot assign strings, but you can assign two structures simply by using an assignment statement. A function can also return a structure.


 

Syntax of structure

struct structure_name 
{
    data_type member1;
    data_type member2;
    .
    .
    data_type memeber;
};
- See more at: http://www.programiz.com/c-programming/c-structures#sthash.vbEuJd1h.dpuf

Syntax of structure

struct structure_name 
{
    data_type member1;
    data_type member2;
    .
    .
    data_type memeber;
};
- See more at: http://www.programiz.com/c-programming/c-structures#sthash.vbEuJd1h.dpuf

Comments