Home     Blog

Structures

Structure is a collection of variables referenced under a common name. Sometimes, some logically related elements need to be treated under one single unit. For instance, the elements that store student information (e.g., rollnumber, name, class, marks and grade) need to be processed together under one roof.

Defining a structure:

  • struct is a required keyword to define a structure.
  • tag-name is the name that identifies the structure.
  • member1, member2 and member3 are the members of the structure.

Complex data types can be constructed from fundamental data types.

Example:

oldcustomer and newcustomer are structure variables of type account.

Here, customer is a 100 element array of structures. Each element of customer is a separate structure of type account. Each customer includes all the members.

Variable declaration:

Structures can contain any C++ data type, including arrays, pointers or other structures.

Referencing Structure Elements

Once a structure has been defined, its members can be accessed with the use of dot (.) operator.

The structure variable name followed by a period (.) and the element name is the name of the specific structure variable.

Initialization of Structure

Structure elements can be initialized in 2 ways,

  1. By initializing every element individually
    oldcoustomer.name = ”Albert” ;
    oldcoustomer.accno = 372 ;
    oldcustomer.balance = 58880.50 ;
  2. By the notation which is used for the initialization of arrays

Nested Structure

A structure element may be either a complex or simple data-type. The simple data-type are any of the fundamental data-types such as int, char, float, double. However, a structure may have elements that are complex i.e. array, structure, etc. Thus, an element of a structure may even be an array or a structure itself. A structure which is a member of another structure, is known as nested structure.

Example:

Program 1:

Accepts data in the structure named student and display the contents.

structures clip image002 Structures

Program 2:

Accepts data into an array of stuture and display the contents.

structures clip image004 Structures

Output:

structures clip image006 Structures

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Daniel Memetic on

Leave a Reply

Related Posts

  • Arrays

    An array is a collection of C or C++ data elements or objects of the same type. Individual elements of an array are referenced using one or more integer indices. A one-dimensional array is defined with a statement such as double vector[50]. This defines (and allocates space for) 50 variables of type double that are [...]...


  • Singly Linked List

    In a singly (one-way) linear linked list, each node is divided into two parts. The first part contains the information of the element. The second part called the linked field or next pointer field contains the address of the next node in the list. A head pointer is used to hold the address of the [...]...


  • Stacks

    Stacks are one of the commonly used data structures. A stack is also known as a last in first out (LIFO) system. It can be considered as a linear list in which insertion and deletion can take place only at one end called the top. This structure operates in much the same way as a [...]...


  • Pointers and Arrays

    Arrays and pointers are very closely linked together. In most contexts, C++ treats the name of an array as if it were a pointer i.e. memory address of some element. C++ interprets an array name as the address of its first element. That is, if age is an int array to hold 10 integers then [...]...


  • Stacks using Dynamic Memory Allocation

    A stack that dynamically allocates memory for each of its elements (just like a linked list) is also known as a linked stack. The array-based representation of a stack suffers from the following limitations. The size of the stack must be known in advance. We may come across a situation when we need to push [...]...