Preprocessor
Typedef

Structure

Structure in C

Structure in C -language is a collection of different data types which are grouped together and each element in a C structure is called member.

Defining a Structure

The syntax for structure is:

struct structure_name
{
  data-type member-1;
  data-type member-2;
  data-type member-3;
  data-type member-4;
};

In our case, let’s name the structure as visitor. The members of the structure in our case are name, phone_number and gmail_id.

So, our structure will look like:


struct visitor { char name[30]; int phone_number;
char gmail_id; };

Accessing Members of Structure

For accessing any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use the keyword struct to define variables of structure type. The following example shows how to use a structure in a program

#include<stdio.h>
#include<string.h>
 
struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( ) {

   struct Books Book1;        /* Declare Book1 of type Book */
   struct Books Book2;        /* Declare Book2 of type Book */
 
   /* book 1 specification */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   /* book 2 specification */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   /* print Book1 info */
   printf( "Book 1 title : %s\n", Book1.title);
   printf( "Book 1 author : %s\n", Book1.author);
   printf( "Book 1 subject : %s\n", Book1.subject);
   printf( "Book 1 book_id : %d\n", Book1.book_id);

   /* print Book2 info */
   printf( "Book 2 title : %s\n", Book2.title);
   printf( "Book 2 author : %s\n", Book2.author);
   printf( "Book 2 subject : %s\n", Book2.subject);
   printf( "Book 2 book_id : %d\n", Book2.book_id);

   return 0;
}

Book 1 title : C Programming
Book 1 author : Nuha Ali
Book 1 subject : C Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Zara Ali
Book 2 subject : Telecom Billing Tutorial
Book 2 book_id : 6495700

Array of Structures

Let’s see an example for 5 students.

#include<stdio.h>
struct student
{
  int roll_no;
  char name[30];
  int phone_number;
};
int main() 
{
  struct student stud[5]; 
  int i;
  for(i=0; i<4; i++)
    {
      printf("Student %d\n",i+1);
      printf("Enter roll no. :\n");
      scanf("%d", &stud[i].roll_no);
      printf("Enter name :\n");
      scanf("%s",stud[i].name);
      printf("Enter phone number :\n");
      scanf("%d", &stud[i].phone_number);
    }
  for(i=0; i<4; i++)
    {
      printf("Student %d\n",i+1);
      printf("Roll no. : %d\n", stud[i].roll_no);
      printf("Name : %s\n", stud[i].name);
      printf("Phone no. : %d\n", stud[i].phone_number);
    }
  return 0;
}

Student 1 
Enter roll no. : 

Enter name : 
sam 
Enter phone number : 
1321 
Student 2 
Enter roll no. : 

Enter name : 
brown 
Enter phone number : 
4321 
Student 3 
Enter roll no. : 

Enter name : 
addy 
Enter phone number : 
4543 
Student 4 
Enter roll no. : 

Enter name : 
max 
Enter phone number : 
3412 
Student 1 
Roll no. : 1 
Name : sam 
Phone no. : 1321 
Student 2 
Roll no. : 2 
Name : brown 
Phone no. : 4321 
Student 3 
Roll no. : 3 
Name : addy 
Phone no. : 4543 
Student 4 
Roll no. : 4 
Name : max 
Phone no. : 3412

Pointers to Structures

You can define pointers to structures in the same way as you define pointer to any other variable

Lets try an example similar to the example that we try in accessing members of structure .

#include<stdio.h>
#include<string.h>
 
struct Books {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

/* function declaration */
void printBook( struct Books *book );
int main( ) {

   struct Books Book1;        /* Declare Book1 of type Book */
   struct Books Book2;        /* Declare Book2 of type Book */
 
   /* book 1 specification */
   strcpy( Book1.title, "C Programming");
   strcpy( Book1.author, "Nuha Ali"); 
   strcpy( Book1.subject, "C Programming Tutorial");
   Book1.book_id = 6495407;

   /* book 2 specification */
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Zara Ali");
   strcpy( Book2.subject, "Telecom Billing Tutorial");
   Book2.book_id = 6495700;
 
   /* print Book1 info by passing address of Book1 */
   printBook( &Book1 );

   /* print Book2 info by passing address of Book2 */
   printBook( &Book2 );

   return 0;
}

void printBook( struct Books *book ) {

   printf( "Book title : %s\n", book->title);
   printf( "Book author : %s\n", book->author);
   printf( "Book subject : %s\n", book->subject);
   printf( "Book book_id : %d\n", book->book_id);
}

Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutorial
Book book_id : 6495700

Comments are closed.