C- Programming
Sujan Karki
    Email: sujan@ioepc.edu.np
     Contact No.: 9819387234
Master’s in Information System Engineering (MscIne) *Ongoing
Bachelor in Computer Engineering – Purwanchal Campus,IOE
        UNIT 8
    STRUCTURE & UNION
2
                      STRUCTURE
    • A structure is a collection of logically related data
       items grouped together under a single name.
    • In structure the individual elements may differ in type,
       that’s why we can regard structure as a heterogeneous
       user-defined data type.
    • The data items enclosed within a structure are known
       as members.
     Syntax:
     struct struct_name {
     data_type mem1;
     data_type mem2;
     .....................
     data_type memn;
3
     };
…
Once structure_name is declared as new data type, then
variables of that type can be declared as:
Struct structure_name structure _variable;
    Example:
    struct student                 struct student
    {                              {
    char name[15];                 char name[15];
    int roll;                      int roll;
    float fee;                     float fee;
    };                             }st1,st2,st3;
    struct student st1,st2,st3;
4
 …
Initialization of structure Variables:
The syntax of initializing structure variables is similar to that of
arrays. All the values are given in curly braces and the number,
order and type of these values should be same as in the structure
template definition.
Example
struct student {
char name[15];
int roll;
float fee;
};
struct student st={“Sonia”,23,1450.50};
5
 …
Accessing members of structure:
For accessing any member of a structure variable, we use the
dot(.) operator which is also known as the period or membership
operator.
Syntax: structure_variable.member or,
structure_variable.member.submember
6
    …
    Array of structures
    We can declare array of structures where each element of
    array is of structure type.
    struct student                        struct student
    {                                     {
    int roll;                             int roll;
    char name[15];                        char name[15];
    char course[15];                      char course[15];
    char sem[15];                         char sem[15];
    };                                    }st[100];
    struct student st[100];
7
    #include<stdio.h>               printf("\n Name\t Course");
    void main() {                   for(i=0;i<n; i++)
    struct student {                {
    int roll;                       printf("\n%s\t%s", st[i].name, st[i].course);
    char name[15];                  }
    char course[15];                }
    char sem[15];
    };
    struct student st[100];
    int i, n;
    printf("\n How many students are there: ");
    scanf("%d", &n);
    for(i=0;i<n; i++)
    {
    printf("\n Enter roll no, name, course and semester: ");
    scanf("%d%s%s%s",&st[i].roll,st[i].name, st[i].course, st[i].sem);
    }
8
    ⚫Initializing array of structure:
    struct student
    {
    int roll;
    char name[15]; // Array as a Member to Structure
    char course[15];
    char sem[15];
    };
    struct student st[5]={
    1, “Sujan”, “Cprogramming”, “first”,
    4, “Kaushar”, “Mechanics”, “fifth”,
    10, “Alisha”, “Java”, “first”,
    30, “Nischal”, “Thermodynamics”, “Seventh”,
    50, “Pramod”, “React”, “first”,
    };
9
 Nested structure: (Structure as a Member to Structure)
     The members of a structure can be of any data type including
     another structure type i.e. we can include a structure within
     another structure. A structure variable can be a member of
     another structure. This is called nesting of structure.
                                         Example:
10
                     Alternative way
     struct student
     {
     int roll;
     char name[15];
     struct date {
     int year;
     int month;
     int day;
     }dob;
     float salary;
     };
     struct student st[100];
11
• Example:
#include<stdio.h>
void main() {
struct student {
int roll;
char name[15];
struct date {
int year;
int month;
int day;
}dob;
};
struct student st[100];
int i, n;
printf("\n How many students are there: ");
scanf("%d", &n);
12
for(i=0;i<n; i++) {
printf("\n Enter roll no, name and date of birth in (year-month-day): ");
scanf(“%d%s%d%d%d",&st[i].roll, st[i].name, &st[i].dob.year,
&st[i].dob.month, &st[i].dob.day);
}
printf(“\n Rollno\tName\tDOB(Year-Month-Day)");
for(i=0;i<n; i++) {
printf("\n%d\t%s\t%d-%d-
%d”,st[i].roll, st[i].name, st[i].dob.year, st[i].dob.month, st[i].dob.day);
}
}
13
 ⚫Pointer to structure :
 We can have pointer to structure, which can point to the starting address of a
 structure variable. These pointers are called structure pointers. While
 accessing structure members through pointers we have to use arrow operator
 (->) which is formed by hyphen symbol and greater than symbol.
14
• Passing structure members to a function:
     #include<stdio.h>                     scanf("%s", d.mon);
                                           printf("Enter year: ");
     struct date {                         scanf("%d", &d.yr);
        int day;                           display(d.day, d.mon, d.yr);
        char mon[15];
        int yr;                            return 0;
     };                                }
     void display(int, char[], int);
                                       void display(int a, char b[], int c) {
     int main() {                        printf("Day = %d\n", a);
        struct date d;                   printf("Month = %s\n", b);
        printf("Enter day: ");           printf("Year = %d\n", c);
        scanf("%d", &d.day);           }
        printf("Enter month: ");
15
 ⚫Pointer an entire structure to function:
16
 ⚫Structure pointer to function:
#include<stdio.h>                    int main() {
                                        struct date d;
struct date {                           printf("Enter day: ");
   int day;                             scanf("%d", &d.day);
   char mon[15];                        printf("Enter month: ");
   int yr;                              scanf("%s", d.mon);
};                                      printf("Enter year: ");
                                        scanf("%d", &d.yr);
void display( struct date *da) {        display(&d);
  printf("Day = %d\n", da->day);
  printf("Month = %s\n", da->mon);       return 0;
  printf("Year = %d\n", da->yr);     }
}
17
 ⚫Pointer array of structure to function:
  #include <stdio.h>
                                          int main() {
                                             struct date d[5];
  struct date {
                                             int i;
     int day;
                                             for (i = 0; i < 5; i++) {
     char mon[15];
                                                printf("Enter day: ");
     int yr;
                                                scanf("%d", &d[i].day);
  };
                                                printf("Enter month: ");
                                                scanf("%s", d[i].mon);
   void display(struct date d[]) {
                                                printf("Enter year: ");
     int i;
                                                scanf("%d", &d[i].yr);
     for (i = 0; i < 5; i++) {
                                             }
        printf("Day = %d\n", d[i].day);
                                             display(d);
        printf("Month = %s\n", d[i].mon);
        printf("Year = %d\n", d[i].yr);
                                            return 0;
     }
                                          }
18
   }
     Difference between Structure & Array
19
             Self Referential Structure
Self Referential structures are those structures that have one or
more pointers which point to the same type of structure, as their
member.
It is also called Pointer as a member to Structure.
Example:
struct node {
int data1;
char data2;
struct node * link;
} ob ;
20
#include <stdio.h>
#include <stdlib.h>
struct Node {
   int data;
   struct Node* next; // Pointer as a member to Structure
};
int main() {
   struct Node n1, n2;
   n1.data = 10;
   n2.data = 20;
   n1.next = &n2;
   printf("Data in Node 1: %d\n", n1.data);
   printf("Data in Node 2 via Node 1: %d\n", n1.next->data);
   return 0;
}
21
     . . . to be continued !!!
22