Structure in C

Definition

  • Structure and Union in C is a data structures/variables that store large no. of data items in contiguous memory locations whose individual elements may differ in their datatypes.
  • structure is a user-defined data type available in C that allows to combining of data items of different kinds.
  • structure is a collection of related data members of the same or different data types variables under a single name at one place in memory.

Characteristics

  • Structures are used to represent a record.
  • A C structure is a convenient way of grouping several pieces of related information together just like a record.
  • A single structure might contain integer elements, floating–point elements, character elements, etc together. Pointers, arrays, functions, unions, and other structures can also be included as elements within a structure.
  • It is a collection of different variables under a single name and each may have different datatype in one place.
  • Structures members can be processed in the same manner as ordinary/simple variables of the same data type.
  • Each variable/data element of a structure is called a member/structure element of the structure. They can hold any number of variables as per need. we can make arrays of structures for large no. of data items and thus structures are ideally useful for creating databases in C for storing data permanently. The individual members can be ordinary variables, pointers, arrays, functions, unions,s or other structures. The member names within a particular structure must be distinct from one another, though a member name can
    be the same as the name of a variable defined outside of the structure.
  • The members of a structure variable can be assigned with initial/static values in much the same manner as the elements of an array. The initial values must appear in the order in which they will be assigned to their corresponding structure members, enclosed in braces, and separated by commas. The general form/syntax is –
                    storage-class struct tag variable = { value1, value 2,——-, value m};

                          example –

                          struct student
                              {
                                    char name [80];
                                    int roll_no;
                                    float marks;
                                    struct dob d1;
                               };
                           static struct student st = { “ Rajiv”, 241, 87.26, 17-12-1911};   
  • Structure and Union can be passed through function and they can also be returned from functions.
  • If s1 and s2 are two structure variables having the same composition then It is possible to copy the values of s1 to s2 simply by using s2=s1;
  • Unlike class, a struct is created on the stack. So, it is faster to instantiate (and destroy) a struct than a class.                

Syntax

  • Declaration of Structure :
    • To declare a structure we use a keyword called struct followed by the structure name or structure tag and within the braces there may be a list of the structure’s member variables.
    • Syntax : 
                    struct structure-tag/name 
                   {
                      datatype variablename1;
                      datatype variablename2;
                      dataype variablename3;
...
...
};

------- OR -------

struct tag
{
member 1;
member 2;
------------
-------------
member m;
}
    • Example 
                     struct student 
{
int rollno;
char stuname[20];
char course[20];
float coursefee;
};
    • To access structure elements easily for reading and writing data from structure, we must use at least one structure variable/structure instance. when the structure variable is declared then memory will be allocated. The amount of memory allocated will be the sum of all the data members (datatype) that take part in the formation of the structure. To create structure variables: –
                   struct student 
{
int rollno;
char stuname[20];
char course[20];
float coursefee;
}x,y;

--------- OR ---------

struct student
{
int rollno;
char stuname[20];
char course[20];
float coursefee;
};
struct student x,y;

                     Here, x and y are two structure variables.

  • To Access Structure Members :
    • Structure members are accessed using the structure member operator (.), also called the dot operator, between the structure name and the member name. This period (.) symbol is an operator, and it is a member of the highest precedence in the group, and its associativity is left-to-right.
    • The syntax for accessing the member of the structure is: –
                              structure_variablename. structure_membername;
                               Example are – 
                               struct student
                                  {
                                      int rollno;
                                      char stuname[20];
                                      char course[20];
                                      float coursefee;
                                  };
                                      struct student x,y;
                                      x.rollno=102;
                                      y.rollno=230;
                                      printf (“%d,%d”, x.rollno, y.rollno);
                                 if a structure member is itself a structure, then a member of the embedded                                       structure can be accessed by writing – 
                                        variable.member.submember.
      • Array of Structures is an array in which each element is a structure. for example –
                                 struct student
                                   {
                                       int rollno;
                                       char stuname[20];
                                       char course[20];
                                       float coursefee;
                                    }x[10];
                                 Here x[10] is an array of structures that is capable of storing 10 records of students. The use of the period operator can be extended to arrays of structure,
                                 by writing –
                                                     arrayofstructure [expression]. member 
                                                    for example –  x[i].rollno;              

    Advantage

    • Structure helps to construct a complex data type that is more meaningful in many places.
    • structure is a convenient way of grouping several pieces of related information.
    • Structure provides the functionality of creating a user-defined data type which is very useful for handling large numbers of data in a row and column manner.
    • A structure also provides the functionality of implementing many data structures such as linked lists which are used to implement trees, graphs, etc.

    Disadvantage

    • It uses comparatively more memory requirements during processing than union.
    • All structure elements are public i.e. no security of data elements at all.
    • All the structure elements are located contiguously in the memory of the computer.

    Union in C

    • Definition
      • Union in C is a user-defined data type that allows us to store different types of data in the same memory location, but only one type at a time.
    • Features
      • All members of a union share the same memory, and the size of the union is determined by its largest member.
      • Unions are used in situations where efficient memory use is crucial, and only one member of the union is needed at any given time.
      • The size of a union is determined by the size of its largest member element.
      • We can only use one member of the union at any moment because updating one member will overwrite the previous value stored in the union.
      • The size of a union is determined by the size of its largest member. The memory allocated to the union will be enough to store the largest member.
    • To declare a Union in C – 
    Syntax:
    union union_name
    {
        data_type1 member1;
        data_type2 member2;
        …
    };
    Example:
    union Student
    {
        int rollno;
        float fee;
        char sname[30];
    };
    • Use/Applications of Union:
      • Unions are useful when we need to save memory, especially in embedded systems or low-memory environments, where we want to store one of several different types of data in the same location.
      • In applications where a variable may hold different types of data at different times, unions can be helpful (e.g., a union holding an integer, a float, or a pointer).

    Loading

    Categories: C

    0 Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.