chevron_left

 

Structures and Functions in C:
I take a word num. If I want to access num, I will write something like this: ez1.num.  Now if we want to save a value in this member, we have to write something like this: 

ez1.num = 2; or ez1.ch = 'z';  

In this sense, we can say that dot operator is used to associate any structure variable with its member. For example, if we want to print the values ​​of the above variable, print  Printf ("ez1.num =% d, ez1.ch =% c / n", ez1.num, ez1.ch ); one of the same type of structure.  The same procedure has to be followed for introducing extra variables, which you use for int or float.  We can introduce data in the structure as well as display this data as an output on the screen. Like simple variables and arrays, structure variables can also be introduced at the beginning of the program.  And they can also be given initial quantities. The procedure for introducing a structure variable is similar to that of an array.  C ++ made this possible. Suppose agent1 and agent2 are two of the same type of structure variables, then we can give the following statement.  agent2 = agent1;  

 More structures in a structure or nested structure.

 Just as more arrays can be created within an array, similarly more structures can be introduced within a structure. This way you can introduce more complex data types.

 Structure and function:

 Structure Variable fungi can be given as an argument and similarly can be returned as output. Sending structure pointers to the function as an argument.  The main reason for its use is the same as when used with a simple variable. In the case of a structure, its members are in the use of a variable.  Can be changed using  The reason is that the name of the array has its address. However, the name of a structure does not have its address and a copy of it is made in the function and then  If you want to give quantities in the original structure, use a pointer variable indicating the actual structure.  

Example:

#include <stdio.h>
struct student {
   char name[50];
   int age;
};

// function prototype
void display(struct student s);

int main() {
   struct student s1;

   printf("Enter name: ");

   // read string input from the user until \n is entered
   // \n is discarded
   scanf("%[^\n]%*c", s1.name);

   printf("Enter age: ");
   scanf("%d", &s1.age);

   display(s1); // passing struct as an argument

   return 0;
}

void display(struct student s) {
   printf("\nDisplaying information\n");
   printf("Name: %s", s.name);
   printf("\nAge: %d", s.age);
}


 

Output will be:

enter name:Tania
enter age:26
Displaying information 
Name :Tania
age :26

Example:

More Posts

Structure variables may be passed as arguments to function. Tania - Sep 7
Problem solving in data structures & algorithms using C pdf. Tania - Sep 12
How to fix C# Error Class does not contain a constructor that takes 0 arguments mhfaisal - May 5
How to swap two string in java without using third variables Tania - Sep 7
Local variables are initialized to zero by default amna - Jun 24
what is the difference between an arguments and parameters Being human - Jun 5
what is the difference between an arguments and parameters Being human - Jun 5
What does slice do with no arguments? Lalit Kumar - Oct 9, 2020
Environment variables and Babel dev - Aug 9
which of the following statements declares alpha to be an array of 25 components of the type int? SharadMagar450 - Aug 15