What is Structure?   The structure is user define data type .structure contain variable and function which can use in main().We can acce...

Structure in C

/
0 Comments
What is Structure?

  •  The structure is user define data type .structure contain variable and function which can use in main().We can access the variable and function of class by dot( . ) operator. The structure is a very important topic because this is one type of class.If you understand structure then you can easily understand the class. We declare structure using struct keyword "." structure. We can declare structure inside of void main() and also outside of void main() both.If you define the structure inside,main() the scope is limited to onlymain(). Any other function cannot see that definition and hence, cannot make use of that structure definition. I will recommend you to declare outside of main().
  • Theory:
  • A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. (Structures are called ``records'' in some languages, notably Pascal.) Structures help to organize complicated data, particularly in large programs, because they permit a group of related variables to be treated as a unit instead of as separate entities.
  • One traditional example of a structure is the payroll record: an employee is described by a set of attributes such as name, address, social security number, salary, etc. Some of these, in turn, could be structures: a name has several components, as does an address and even a salary. Another example, more typical for C, comes from graphics: a point is a pair of coordinate, a rectangle is a pair of points, and so on.
  •  



Why we use Structure?

  • Suppose In school there are lots of students and every student have a name, roll no and standard.It is not possible to make variables for all student. So we use structure.In structure, we no need to create variables for each student but we have made reference variable for all student
  • We can make variable, function etc in structure. But we can use it only in main () function.when we call any function or variable of structure in main() function .when compile function first compiler read main() function after analising it will go to structure and fetch it defination.It is good for our memory why ? beacuse we are not loading whole program in memory if compile not find declaation in it will go in structure.


Syntax of structure
struct addrress
{
   datatype variable-name;
datatype variable-name;
datatype variable-name;
};
  • The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces. An optional name called a structure tag may follow the word struct (as with point here). The tag names this kind of structure, and can be used subsequently as a shorthand for the part of the declaration in braces.
  • The variables named in a structure are called members. A structure member or tag and an ordinary (i.e., non-member) variable can have the same name without conflict, since they can always be distinguished by context. Furthermore, the same member names may occur in different structures, although as a matter of style one would normally use the same names only for closely related objects.
How to use it in a void main()?

  • we can make structure reference variable in void main() as well as just after declaring the structure.
  • A member of a particular structure is referred to in an expression by a construction of the form
  • structure-name.member;
  • The structure member operator ``.'' connects the structure name and the member name. To print the coordinates of the point pt, for instance,
  • In a first way, we make structure reference variable inside in main() function
  • In this reference variable, we can use in only void main( ) function and scope of reference variable will be main fuction

→(1st way) Inside of void main() of

main()
{
struct Structure-name refrence-variable-name;
refrence-variable-name.variable-name = 12;
}

  • In second way we will create refrence variable just fter defining in structure.
→(2nd way)Just after declaring the structure.
struct addrress
{
   datatype variable-name;
}a;/* where a is refrence-variable-name*/



Program :Students program

Suppose there are 5 students in class and every student have a name, roll no and
standard.In this program we make the structure and structure contain the variable
name, roll no and standard.And we take input from the user for all student.and
display the information of the student.

#include <stdio.h>
#include <conio.h>
struct student {
char name[10]; int roll; int std; // member variable
} s[5];/*making refrence varible on student stucture in the form of array*/
void main() {
int i;
clrscr();
printf("Enter information of students:\n"); // storing information
for(i=0; i<5; ++i) {
printf("\nEnter roll number");
scanf("%d",s[i].roll);/* taking roll number */
printf("Enter name: ");
scanf("%s",s[i].name);/* taking Name */
printf("Enter Standard: ");
scanf("%d",&s[i].std);/* taking Standard */
printf("\n");
}
printf("Displaying Information:\n\n"); // displaying information
for(i=0; i<10; ++i)
for(i=0; i<10; ++i) {
printf("\nRoll number: %d\n",s[i].roll);
printf("\n Name:%s ",s[i].name);
printf("\n Standard: %d",s[i].std);
printf("\n");
}
getch();//to hold output on screen
}

Note we can make reference variable in void main( )


Output:






No comments:

Cybercry 2018. Powered by Blogger.

Popular Posts