Structures in C Contents Structures Declaration Of Structure





![Another way of creating sturcture variable is: struct person { char name[50]; int cit_no; Another way of creating sturcture variable is: struct person { char name[50]; int cit_no;](https://slidetodoc.com/presentation_image_h2/10ae2f107d740e3798fb94e2ff361716/image-6.jpg)





![Example #include "stdio. h" #include "conio. h" struct adress { char ct[10]; char dist[10], Example #include "stdio. h" #include "conio. h" struct adress { char ct[10]; char dist[10],](https://slidetodoc.com/presentation_image_h2/10ae2f107d740e3798fb94e2ff361716/image-12.jpg)
![{ printf("Enter [%d]st Employee's Name, Age, salary : : ", i); scanf("%s%d%d", e[i]. name, { printf("Enter [%d]st Employee's Name, Age, salary : : ", i); scanf("%s%d%d", e[i]. name,](https://slidetodoc.com/presentation_image_h2/10ae2f107d740e3798fb94e2ff361716/image-13.jpg)


- Slides: 15

Structures in C

Contents Ø Structures Declaration Of Structure variable Ø Accessing members of a structure Ø Keyword typedef while using structure Ø Structures within structures Ø Example Ø Result Ø

Structures Structure is the collection of variables of different types under a single name for better handling. For example: You want to store the information about person about his/her name, citizenship number and salary. You can create these information separately but, better approach will be collection of these information under single name because all these information are related to person. BACK

Structure Definition Structure in C: Keyword struct is used for creating a structure. Syntax of structure: structure_name { data_ type member 1; data_ type member 2; . . data_ type member; }; BACK

Declaration Of Structure variable When a structure is defined, it creates a user-defined type but, no storage is allocated. For the above structure of person, variable can be declared as: struct person { char name[50]; int cit_no; float salary; }; Inside main function: struct person p 1, p 2, p[20];
![Another way of creating sturcture variable is struct person char name50 int citno Another way of creating sturcture variable is: struct person { char name[50]; int cit_no;](https://slidetodoc.com/presentation_image_h2/10ae2f107d740e3798fb94e2ff361716/image-6.jpg)
Another way of creating sturcture variable is: struct person { char name[50]; int cit_no; float salary; }p 1 , p 2 , p[20]; NOTE: - In both cases, 2 variables p 1, p 2 and array p having 20 elements of type struct person are created. BACK

Accessing members of a structure There are two types of operators used for accessing members of a structure. q Member operator(. ) q Structure pointer operator(->) Ø Any member of a structure can be accessed through member operator(. ) as: structure_variable_ name. member_name Example: Suppose, we want to access salary for variable p 2. Then, it can be accessed as: p 2. salary

Ø In structure, to actually access the information stored inside the structure that is pointed to, we can use the ‘->’ operator in place of the ‘. ’ operator. Example: #include <stdio. h> struct xampl { int x; }; int main() { struct xampl structure; struct xampl *ptr; structure. x = 12; ptr = &structure; printf( "%dn", ptr->x ); getchar(); } BACK

Keyword typedef while using structure in C language. Example: typedef struct complex { int imag; float real; }comp; Inside main: comp c 1, c 2; NOTE: typedef keyword is used in creating a type comp(which is of type as struct complex). Two structure variables c 1 and c 2 are created by this comp type. BACK

Structures within structures Structures can be nested within other structures in C programming. struct complex { int imag_value; float real_value; }; struct number { struct complex c 1; int real; }n 1, n 2; NOTE: for accessing imag_value for n 2 structure variable then, structure member n 1. c 1. imag_value is used.

Example: struct date { int day; int month; int year; }; struct company { char name[20]; long int employee_id; char sex[5]; int age; struct date dob; }; BACK
![Example include stdio h include conio h struct adress char ct10 char dist10 Example #include "stdio. h" #include "conio. h" struct adress { char ct[10]; char dist[10],](https://slidetodoc.com/presentation_image_h2/10ae2f107d740e3798fb94e2ff361716/image-12.jpg)
Example #include "stdio. h" #include "conio. h" struct adress { char ct[10]; char dist[10], state[5]; long int pin; }; struct emp { char name[10]; int age, sal; struct adress a; }; void main() { struct emp e[2]; int i; clrscr(); for(i=0; i<=1; i++)
![printfEnter dst Employees Name Age salary i scanfsdd ei name { printf("Enter [%d]st Employee's Name, Age, salary : : ", i); scanf("%s%d%d", e[i]. name,](https://slidetodoc.com/presentation_image_h2/10ae2f107d740e3798fb94e2ff361716/image-13.jpg)
{ printf("Enter [%d]st Employee's Name, Age, salary : : ", i); scanf("%s%d%d", e[i]. name, &e[i]. age, &e[i]. sal); printf("n. Enter city, district, state & pincode : : "); scanf("%s%s%s%ld", e[i]. a. ct, e[i]. a. dist, e[i]. a. state, &e[i]. a. pin); } for(i=0; i<=1; i++) { printf("n[%d]st Employee's Name : : %s", i, e[i]. name); printf("n[%d]st Employee's Age : : %d ", i, e[i]. age); printf("n[%d]st Employee's Salary : : %d", i, e[i]. sal); printf("n[%d]st Employee's City : : %s ", i, e[i]. a. ct); printf("n[%d]st Employee's District : : %s", i, e[i]. a. dist); printf("n[%d]st Employee's State : : %s", i, e[i]. a. state); printf("n[%d]st Employee's Pin : : %ld", i, e[i]. a. pin); } getch(); } BACK

Result BACK

THANKS BACK TO INDEX