Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Wednesday, March 11, 2020

Structure in C

C Structure | welcome2protec.com

Structure are the non-primitive, linear data structure. It is basically defined as a group of variables of different data types with a single name (OR you can say Structure is a collection of dissimilar elements). It is quite opposite to array.


How to define structure?

Defining structure means creating new data type (user defined data type). You can create a structure by using 'struct' keyword.

Syntax:

struct structureName
  {
     dataType member1;
     dataType member2;
     ...
  };

Example:

struct student
  {
      char name[50];
      int rollno;
      float fee;
   };

In the above syntax, struct_name can be anything of your choice. Members data type can be same or different. Once we have declared the structure we can use the struct_name as a data type like int, float etc. For example: int var (here, var is a variable of int type) same way you can use struct student s1 (here, s1 is a variable of student data type).


Declaration of structure variable:

Before creating structure variables, you need to create its data type first. For creating new data type you can see in the above, how to define structure?

There are two ways to declare a structure variable:

1) 1st way to declare a structure variable:

Syntax:
  struct structure_name variable_name;

Example:
  struct student s1, s2, s3;

Description:

struct: is a keyword used for defining structure or can say for creating a new data type.

student: is a name of structure, which can be used as a data type like int, float etc. This name could be anything as per your choice.

And s1, s2 and s3: are structure variables of student type.

Remember: When a structure is defined, no any storage or memory spaces is allocated until we declare structure variables.


2) 2nd way to declare a structure variable:

At the end of the structure definition, you can declare a structure variable before the final semicolon, you can specify one or more structure variables although this is purely optional and users personal choice.

struct structureName
  {
     char name[50];
     int rollno;
     float fee;
   } s1, s2, s3;

Accessing Members of the Structure:

There are two ways to access members of the structure.

  1. . Dot or member operator.
  2. -> Structure pointer operator.

Suppose you want to access the 'fee' of s2. Here, how you can access it.

Syntax:
   Variable_name Member_name;
  
Example:
   s2.fee;

Example 1: Structure

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

struct employee{
  int id;      
  char name[50];   
}e1;  //declaring e1 variable for structure 

int main( )    
{        
   e1.id=0786; //Assigning employee id   
   strcpy(e1.name, "Ahmad Irshad"); //copying string into char array    
    
   //printing first employee information
   printf( "Employee id : %d\n", e1.id);    
   printf( "Employee name : %s\n", e1.name);
   
   return 0;  
}

Output:

Employee id : 0786
Employee name : Ahmad Irshad

Example 2: Structure

#include <stdio.h> 
 
/* Creating a structure here, the name of the structure is StudentData.*/
struct StudentData{
  char *stu_name;
  int stu_id;
  int stu_age;
};

int main()
{
  /*Creating a variable 'student' of StudentData type */
  struct StudentData student;

  /*Assigning the values of each struct member here*/
  student.stu_name = "Ahmad Irshad";
  student.stu_id = 0786;
  student.stu_age = 30;

  /* Displaying the values of struct members */
  printf("Student Name is: %s", student.stu_name);
  printf("\nStudent Id is: %d", student.stu_id);
  printf("\nStudent Age is: %d", student.stu_age);
  
  return 0;
}

Output:

Studet Name is: Ahmad Irshad 
Student ID is: 0786
Student Age: 30

Use of typedef (stands for Type definition) in Structure:

Typedef makes the code short and improves readability. In the above discussion we have seen that while using 'struct' every time we have to use the lengthy syntax, which makes the code confusing, lengthy, complex and less readable. The simple solution to this issue is use of typedef. It is like an alias of 'struct'.


Code without typedef:

struct Distance{
    int feet;
    float inch;
};

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

Code with typedef:

typedef struct Distance{
    int feet;
    float inch;
} dtc;

int main() {
    dtc d1, d2;
}

Instead of using struct distance d1,d2 every time to declare variables, you can simply use like this dtc d1, d2 the one we created by using typedef.









Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us