Skip to main content

Structure

STRUCTURE:

A structure is a collection of variables of different types under a single name or in other words structure is a user defined data type. Structures can be used like any other variable. We can make a struct array or used as pointers.

SYNTAX:

struct structure_name{
type1 id_list1 ;
type2 id_list2 ;
.
.
.
typen id_listn ;
} struct_type ;
EXAMPLE:
struct number{
int num;
char name[10];
};

STRUCTURES WITH FUNCTIONS:

• Stucture variables are passed by value by default.
• To pass a structure by reference, pass its address. Arrays of structures—like all other arrays—are automatically passed by reference.
• To pass an array by value, create a structure with the array as a member. Structures are passed by value, so the array is passed by value.

Implementation of Structure in C:

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

//structure declaration 
struct employee { 
   char name[25]; 
   int id_number; 
   int age; float salary; 
} emp1 = {"Dummy1",1,24,20000}; 

int main() 
{ 
   struct employee emp2; 
/* Structure variable declaration. There is now an emp2 variable that has modifiable variables inside it.*/ 
/* Structure members can be accessed using direct component selection operator or dot operator as shown in the following statements */ 

   strcpy(emp2.name,"Dummy2"); 
   emp2.age = 22; 
   emp2.id_number = 2; 
   emp2.salary = 12000.21; 
   struct employee emp3; 

/* Initialize the structure variable emp3 from input data as follows */ 
   
   printf("Enter the name of employee: " ); 
   gets(emp3.name); 
   printf("Enter the id number of employee: " ); 
   scanf("%d",&emp3.id_number); 
   printf("Enter the age of employee: " ); 
   scanf("%d",&emp3.age); 
   printf("Enter the salary of employee: " ); 
   scanf("%f",&emp3.salary); 

/*A new copy of a structure’s value can be made by simply assigning one structure to another as in the following statement */ 
  
   struct employee emp4 = emp1;

//Displaying the content for each employee 

   printf("\n%s is %d years old and has %d id number and %.2f salary.\n",emp1.name, emp1.age, emp1.id_number,emp1.salary); 
   printf("\n%s is %d years old and has %d id number and %.2f salary.\n",emp2.name, emp2.age, emp2.id_number,emp2.salary);
   printf("\n%s is %d years old and has %d id number and %.2f salary.\n",emp3.name, emp3.age, emp3.id_number,emp3.salary);  
   printf("\n%s is %d years old and has %d id number and %.2f salary.\n",emp4.name, emp4.age, emp4.id_number,emp4.salary); 

   return 0; 

}

Comments

Popular posts from this blog

Data Types

DATA TYPES in C/C++: Since C and C++ are strongly type languages, therefore every thing must be defined. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Data Types Description C-Keyword Format Specifier integer Integers are whole numbers that can have both positive and negative values but no decimal values.   int   %d or %i Float Floating type variables can hold real numbers precision of 6 digits. float %f Double can hold real numbers with the precision of 14 digits. double %f Character data type allows a variable to store only one character. char %c Implementation in C: #include<stdio.h> int main() { printf(“%d n”,18); //this will print integer printf(“%fn”,32.56); //this will print decimal number printf(“%d %d %dn”,32,56,89); // %d is a place holder for integer and it provide place for one integer printf(“%ld...

Conditional Statement

Conditional Statement: A conditional statement is an if-then statement which means that if some statement is true then it follows the if-block of code and if the condition is false then it follows the else-block of code. In the programming world, there are two types of conditional statement namely if-else and switch. if-else Statement: Syntax: if (condition 1) { } else if (condition 2) { } else { }   Implementation of If-else statement in C: Q: Write a program to find the greatest of three numbers. #include<stdio.h> int main() { int a,b,c; printf(“Please Enter First Number “); scanf(“%d”,&a); printf(“Please Enter Second Number “); scanf(“%d”,&b); printf(“Please Enter Third Number “); scanf(“%d”,&c); if (a>b && a>c) { printf(“The Greatest Number is %d”,a); } else if (b>c) { printf(“The Greatest Number is %d”,b); } else { pr...

Tic-Tac-Toe Implementation in Cpp (C++) Step by Step