Showing posts with label Structure. Show all posts
Showing posts with label Structure. Show all posts

Sunday, March 31, 2019

Nested Structure in C

C programming provides important feature called nesting of Structure. A structure can be nested inside another structure. This feature is useful to maintain complex data structure. Following example explains how a Person structure is used in a Student structure.

Nesting of Structure in C

#include<stdio.h>

//Declaring a structure student
struct student
{
    struct person
    {
        char firstname[20];
        char lastname[20];
        char dob[10];
    } p ;

    int rollno;
    float marks;
};

int main()
{
    struct student s1;

    printf("*** READING STUDENT'S DETAILS *** \n\n");

    printf("Enter firstname: ");
    scanf("%s", s1.p.firstname);

    printf("Enter lastname: ");
    scanf("%s", s1.p.lastname);

    printf("Enter DoB(dd-mm-yyyy): ");
    scanf("%s", s1.p.dob);

    printf("Enter roll no: ");
    scanf("%d", &s1.rollno);

    printf("Enter marks: ");
    scanf("%f", &s1.marks);

    printf("\n*** PRINTING DETAILS OF STUDENT FROM STRUCTURE ***\n\n");

    printf("Name: %s %s\n", s1.p.firstname, s1.p.lastname);
    printf("DOB: %s\n", s1.p.dob);
    printf("Roll no: %d\n", s1.rollno);
    printf("Marks: %.2f\n", s1.marks);

    return 0;
}

Output of Program

*** READING STUDENT'S DETAILS ***

Enter firstname: Purv
Enter lastname: Patel
Enter DoB(dd-mm-yyyy): 10-12-2008
Enter roll no: 101
Enter marks: 85

*** PRINTING DETAILS OF STUDENT FROM STRUCTURE ***

Name: Purv Patel
DOB: 10-12-2008
Roll no: 101
Marks: 85.00


* * * * *

Recommended Readings: C Structure



Monday, October 1, 2018

Reading Structure from file

Following program will read a Structure (customer) data from a Customer data file.

#include <stdio.h>
#include <stdlib.h>

struct customer {
   char  fname[30],lname[30];
   int   ac_num;
   float ac_balance;
};

void main ()
{
   FILE *fp;
   struct customer input;

   fp = fopen ("customer.dat","r");
   if (fp == NULL)
   {
      printf("\nFile reading error\n\n");
      exit (1);
   }

   while (fread (&input, sizeof(struct customer), 1, fp))
      printf ("Name = %s %s,   Acct Num = %d   Balance = %8.2f\n",
              input.fname, input.lname, input.ac_num, input.ac_balance);
}

Output of Program

Name = ABC PATEL,   Acct Num = 101   Balance = 55000.00
Name = XYZ SHAH,   Acct Num = 102   Balance = 50000.00

Writing Structure to data file



Following program will declare a Structure "customer". Program reads customer information and then writes into a "customer.dat" file.

#include <stdio.h>
//Define structure to store customer data.
struct customer
{
   char fname[30];
   char lname[30];
   int  ac_num;
   float ac_balance;
};

void main ()
{
   FILE *fp;
   //declare structure(customer) variable
   struct customer input;
   // open customer file for writing
   fp = fopen ("customer.dat","w");
   if (fp == NULL){
      printf("\nFile opening error..\n\n");
      exit (1);
     }

   printf("Enter \"exit\" as First Name to stop reading user input.");

   while (1)
     {
      printf("\nFirst Name: ");
      scanf ("%s", input.fname);
     
      if (strcmp(input.fname, "exit") == 0)
         exit(1);
     
      printf("Last Name : ");
      scanf ("%s", input.lname);
      printf("Account Number  : ");
      scanf ("%d", &input.ac_num);
      printf("Balance   : ");
      scanf ("%f", &input.ac_balance);

      // write customer data to customer.dat  file
      fwrite (&input, sizeof(struct customer), 1, fp);
     }
}

Output of program

Enter "exit" as First Name to stop reading user input.
First Name: ABC
Last Name : PATEL
Account Number  : 101
Balance   : 55000

First Name: XYZ
Last Name : SHAH
Account Number  : 102
Balance   : 50000


First Name: exit

Friday, April 14, 2017

Accessing structure using Pointer

This program will use members of structure using pointer.

#include <stdio.h>
struct student
{
   int rollno;
   float age;
};

int main()
{
    struct student *ptr_student, student1;
    // Referencing pointer to memory address of student1
ptr_student = &student1;            

    printf("Enter rollno: ");
    scanf("%d",&(*ptr_student).rollno);

    printf("Enter age: ");
    scanf("%f",&(*ptr_student).age);

    printf("Student Details: ");
    printf("%d,%f",(*ptr_student).rollno,(*ptr_student).age);

    return 0;
}

Output of program

Enter rollno: 101
Enter age: 21.5
Student Details: 101,21.50

Thursday, April 13, 2017

Dynamic Memory Allocation for Structure


#include <stdio.h>
#include<stdlib.h>

struct book
{
   int id;
   char title[30];
};

int main()
{
   struct book *ptr;
   int i, n;
   printf("Enter number of books: ");
   scanf("%d", &n);

   // Dynamic Memory allocation for n books
   
   ptr = (struct book*) malloc (n * sizeof(struct book));

   for(i = 0; i < n; ++i)
   {
       printf("Enter book ID:");
       scanf("%d", &(ptr+i)->id);
       printf("Enter book Title:");
       scanf("%s", &(ptr+i)->title);
   }

   printf("\nBook Details(ID, Title):\n");

   for(i = 0; i < n ; ++i)
       printf("%d, %s\n", (ptr+i)->id, (ptr+i)->title);

   return 0;
}

Output of Program

Enter number of books: 3
Enter book ID:101
Enter book Title:Computer
Enter book ID:102
Enter book Title:Science
Enter book ID:103
Enter book Title:Security

Book Details(ID, Title):
101, Computer
102, Science
103, Security

Wednesday, April 12, 2017

Array of Structure

This program will demonstrate Array of Structure.

#include <stdio.h>
#include <string.h>

struct Book{
   int   book_id;
   char  book_title[30];
   char  book_publisher[30];
   char  book_subject[30];
};

int main( ) {

// Declare 3 variables of Book structure

 struct Book myBook[3];  
int i;

//Initialise values to myBook variable
for(i=0; i<3; i++)
{
printf("\nEnter Details of Book%d:\n",i+1);
printf("ID:");
scanf("%d",&myBook[i].book_id);

printf("Title:");
  scanf("%s",myBook[i].book_title);
  
  printf("Publisher Name:");
    scanf("%s",myBook[i].book_publisher);
  
    printf("Subject:");
    scanf("%s", myBook[i].book_subject);
}
   
   /* print myBook details */
   for(i=0; i<3; i++)
   {
    printf("\nDetails of Book%d:\n",i+1);
    printf( "ID: %d\n", myBook[i].book_id);
    printf( "Title: %s\n", myBook[i].book_title);
    printf( "Publisher Name : %s\n", myBook[i].book_publisher);
    printf( "Subject : %s\n", myBook[i].book_subject);
    printf("\n");
   }
   return 0;
}

Output of program


Enter Details of Book1:
ID:101
Title:Math-1
Publisher Name:ABC
Subject:Mathematics

Enter Details of Book2:
ID:102
Title:DataScience
Publisher Name:XYZ
Subject:Science

Enter Details of Book3:
ID:103
Title:Security
Publisher Name:ABC
Subject:Cryptography

Details of Book1:
ID: 101
Title: Math-1
Publisher Name : ABC
Subject : Mathematics


Details of Book2:
ID: 102
Title: DataScience
Publisher Name : XYZ
Subject : Science


Details of Book3:
ID: 103
Title: Security
Publisher Name : ABC
Subject : Cryptography


Sunday, March 5, 2017

Difference between Structure and Union

Structure: structure in C language is a collection of variables with a single name. These variables can be of different types. 

Union: It is a special data type in C which allows us to store different data types in the same memory location.


 Structure

 Union
struct keyword is used to define Structure in C.

union keyword is used to define Union in C. 
Members of structure do not share memory. 

Members of union shares the memory. 

 Members of structure can be accessed at any time individually.

At a time only one member of union can be accessed.
Syntax:

struct structure_name
{
  datatype var1;
  datatype var2;
  ---
  ---
  datatype varn;

}struct_variable_name;
Syntax:

union union_name
{
  datatype var1;
  datatype var2;  ---
  ---
  datatype varn;

}union_variable_name; 


Saturday, June 18, 2016

C program to show use of Structure concept.

//C program to demonstrate use of Structure for storing a Book detains for Library Program.

#include <stdio.h>
#include <string.h>

struct Book{
   int   book_id;
   char  book_title[30];
   char  book_publisher[30];
   char  book_subject[30];
};

int main( ) {

   struct Book myBook;  // Declare variable of type Book

   //Initialise values to myBook variable
   myBook.book_id=101;
 
   printf("Enter Book Title:");
   gets(myBook.book_title);
 
   printf("Enter Book Publisher Name:");
   gets(myBook.book_publisher);
 
   printf("Enter Book Subject:");
   gets(myBook.book_subject);
 
   /* print myBook details */
   printf( "Book ID: %d\n", myBook.book_id);
   printf( "Book Title: %s\n", myBook.book_title);
   printf( "Book Publisher Name : %s\n", myBook.book_publisher);
   printf( "Book Subject : %s\n", myBook.book_subject);
   return 0;
}

Output of program

Enter Book Title:Computer Studies
Enter Book Publisher Name:ABC India
Enter Book Subject:Computer Science
Book ID: 101
Book Title: Computer Studies
Book Publisher Name : ABC India
Book Subject : Computer Science