C tutorial: Structure in C programming

Programming languages or concepts

Structure in C programming


this tutorial we are learning structure in C following is some points covered in this tutorial:-

 How to create a structure?
How to declare structure variables?
How to initialise structure elements?
How to access structure elements?
Structure with structure / nested structure
Structure and pointer
Structure :: typedef keyword
Copy a structure into another structure


Structure:-

  •  Structure define using struct keyword.
  • When a variable is associated with a structure the compiler allocates the the memory for each member. the size of structure is greater than or equal to the sum of size of its member.
  •  Each member within structure is assigned unique storage area of structure.
  • Individual members can be accessed at a time.

   
  • How to create a structure:-

   using the struct keyword can create complete data structure in C.
    Syntax:
                    Struct <StructName>
                     {
                          // Variables
                         .......
                      }

You can declare variables that have as type that structure by adding them after the closing curly bracket before the semicolon.
Ex.
       Struct StructDemo 
         {
              int age;
              char *name;
          } mark;

  • How to declare structure variable :-

    Memory will be allocated only if a variable is a declared for structure.
    a structure variable can either be declared with the structure declaration or as a separate declaration like basic type.

Basic data types:-

          Struct Demo{
                  int a, b;
            };
             int main ( )
             {
                Struct Demo var;
              }

  • How to any slide structure members:-

   Memory is allocated only when variable are created, structure members can be initialised using curly brackets/ braces '{  }' .

Ex. 
        Struct StructDemo 
         {
             int a, b,c;
           };
            int main ( )
            {
               Struct StructDemo Var= { 2,7,99};
            }

  • How to access structure elements:-

    Structure members are accessed using dot (.) operator.
Syntax:
      Structure Variable Name.MemberName;

Ex.

#include<studio.h>
Struct StructDemo
{
    int a, b, c;
};
int main ()
{
   
         Struct StructDemo Var = { 9, 99, 999};
        Var.a = 999;
         Var.c = 9;
       printf (" a=%d, b=%d, c=%d", Var.a , Var.b, Var.c );
       return 0;
}

  • Structure within structure:-

   Its also called as nested structure.
    structure within structure means nested of the structure where one struct variable is used in another structure definition for a new structure is define inside the structure.
Ex.
       Struct Studentdetails 
        {
           char name[30]; 
            char address [50];
          Struct {
                          int marks;
                          int id;
                        }Strudent_details;
         }

  • Structure and pointer:-


A structure can contain pointers as structure members. You can create an invoice structure that contains  invoice_number as a pointer:

struct invoice{

char* invoice_number;

char date[20];

};

 struct address billing_addr;

struct address *pa = &billing_addr;

  • Structure :: typedef keyword:-


We use the typedef keyword to create an alias name for data types. It is commonly used with structures to simplify the syntax of declaring variables.

Ex.

struct Distance{
    int feet;
    float inch;
};

int main() {
    struct Distance d1, d2;
}

  • Copy a structure into another structure


One of major advantage of structure is that you can copy it with the assignment ( =) operator:


struct_intance1 = struct_intance2
Notice that some old C compilers may not support structure assignment so you have to assign each structure member one by one.
close