Chapter 14
Chapter 14
Chapter 14
Structures john.salary = 3500.59; this is assigning the value 3500.59 to salary of john. Program: struct personal { char name[20]; int day; int month[10]; int year; float salary; }; main() { struct personal p; scanf(%s%d%s%f,p.name,&p.day,p.month,&p.year,&p.salary); printf(%s%d%s%f,p.name,p.day,p.month,p.year,p.salary); } Output: John 10 March 1987 4500 John 10 March 1987 4500.00 Type-defined structure We can use the keyword typedef to define a structure as follows: typedef struct { type member1; type member2; }type_name; for example, typedef struct { char title[20]; char author[20]; int pages; float price; } book1,book2; here book1, book2 are structure variables for the above structure. Structure Initialization Struct student {
Structures int weight; float height; }student1={58,167.65}; This assigns the value 58 to student1.weight and 167.65 to student1.height. there is a oneto-one correspondence between the members and their initializing values. Rules for initializing structures 1. We cannot initialize individual members inside the structure template. 2. The order of values enclosed in braces must match the order of members in the structure definition. 3. It is permitted to have a partial initialization. We can initialize only the first few members and leave the remaining blank. The uninitialized members should be only at the end of the list. 4. The uninitialized members will be assigned default values as follows: Zero for integer and floating point numbers. \0 for characters and strings. 7.2 Arrays of structures We use structures to describe the format of a number of related variables. For example, struct marks { int subject1; int subject2; int subject3; }; main() { struct marks student[3]={{45,68,81},{75,53,69},{57,36,71}}; } This declares the student as an array of three elemens student[0], student[2], and student[2]. Program: struct marks { int subject1; int subject2; int subject3; }; main() { struct marks student[3]={{45,68,81},{75,53,69},{57,36,71}}; int i; for(i=1;i<=3;i++) {
Structures student[i].total=student[i].subject1+ student[i].subject2+ student[i].subject3; } for(i=1;i<=3;i++) { printf(\n Total of student[%d] is:%d,i,student[i]); } } Output: Total student[1] is:193 Total student[2] is:197 Total student[3] is:164 Arrays within structures C permits the use of arrays as structure members. Similarly, we can use arrays of any type within structures. For example, struct marks { int roll_no; int subject[3]; }; Here the structure member subject contains three elements, subject[0], subject[1], and subject[2]. These elements can be accessed using appropriate subscripts. That is, student[1].subject[2]; would refer to the marks obtained in the third subject by the second student. Program: main() { struct marks { int sub[3]; int total; }; struct marks student[3]={45,67,81,0,75,53,69,0,57,36,71,0}; int j,k; for(j=0;j<=2;j++) { student[j].total=0; for(k=0;k<=2;k++) { student[j].total=student[j].total+student[j].sub[k]; } } for(j=0;j<=2;j++) { printf(\n Total of student[%d]=%d,j,student[j].total);
Structures } } Output: Total of student[0]=193 Total of student[1]=197 Total of student[2]=164 Structures within structures Structures within a structure means nesting of structures. For example, struct salary { char name[10]; char department; int basic_pay; float da; float hra; float ta; } employee; This structure defines name, department, basic_pay and three kinds of allowances. We can group all the items related to allowance together and declare them under a substructure as shown below: Struct salary { char name[10]; char department; struct { int basic_pay; float da; float hra; float ta; }allowance; } employee; the salary structure contains a member named allowance which itself a structure with three members. The members contained in the inner structure can be referred to as: employee.allowance.da employee.allowance.hra employee.allowance.ta we can also use tag names to define inner structures. Example: struct pay { float da; float hra; float ta; }allowance; struct salary
Structures { char name[10]; char department; int basic_pay; struct pay allowance; }employee; 7.3 Structures and functions C supports the passing of structure values as arguments to functions. The general format of sending a copy of a structure to the called functions: function_name(structure_variable_name); the called function takes the following form: data_type function_name(struct_type st_name) { ........ ........ return(expression); } Program: struct product { char name[20]; float price; int quantity; }; struct product update(struct product prd, float , int q); float bill(struct product); main() { float amount; struct product item={XXX,25.75,12}; amount=bill(item); printf(\n The bill for item is:%d,amount); new_item=update(item,10,12); printf(\n Updated values of item is); printf(\n Name :%s,new_item.name); printf(\n Price :%f,new_item.price); printf(\n Quantity :%d,new_item.quantity); } float bill(struct product prd) { return(prd.price*prd.quantity); } struct product update(struct product prd, float p, int q) { strcpy(prd.name,YYY);
Structures prd.p+=p; prd.q+=q; return(prd); } Output: The bill for item is:309.0000 Updated values of item is Name :YYY Price :35.75000 Quantity :24 7.4 Unions Unions follow the same syntax as structures. But there is major distinction between them in terms of storage. In structures, each member has its own storage location, whereas all the members of a union use the same location. This implies that, although a union may contain many members of different types, it can handle only one member at a time. Example format for declaring a union is: union item { int m; float x; char c; }code; To access a union member, we can use the same syntax that we use for structure members. That is, code.m code.x code.c During accessing, we should make sure that we are accessing the member whose value is currently stored. For example, code.m=57; code.x=12.23 printf(%d,code.m); would produce error. Pointers and Structures Pointers to structures may also be declared and assigned. For example, struct order { int qty; float price; }item[3],*ptr; ptr=item; This assignment would assign the address of item[0] to ptr.
Structures A special member access operator, "->"(called arrow operator), is used to access the members of a structure via a pointer. That is, ptr->qty ptr->price Program: struct invent { char name[20]; int qty; float price; }; main() { struct invent item[3],*ptr; for(ptr=item;ptr<item+3;ptr++) scanf(%s%d%f,ptr->name,&ptr->qty,&ptr->price); for(ptr=item;ptr<item+3;ptr++) { printf(\n Item name is:%s,ptr->name); printf(\n Item quantity is:%d,ptr->qty); printf(\n Item price is:%f,ptr->price); } } Output: Item name is: Pens Item quantity is: 5 Item price is: 20.50 Item name is: Books Item quantity is: 3 Item price is: 150.00 Item name is: Chair Item quantity is: 1 Item price is: 350.00 Summary A structure is a collection of one or more variables, possibly of different types. A structure declaration starts with the keyword struct, which introduces the declaration. Structure members can be accessed using the member access operator .. The structure declaration defines a new data type called user defined data type. Nested structures are nothing but structures within structures. Pointers to structures are just like pointers to other variables. Members can be accessed through the pointer variable using the member access operator ->.
Structures Structures may be passed to functions either by value or reference. A union is also a user defined data type that may hold group of different data types. A union declaration is similar to a structure declaration.
Review Questions State whether true or false 1. Members of the structure must be of the same data type. 2. We can initialize the structure member within a structure template. 3. A union is used to declare the group of elements of different data type. 4. The size of union is same as the structure. 5. We can pass the structure element to a function. Fill in the blanks 1. The uninitialized members of the structure should be at the __________ list. 2. __________ can handle only one member at a time. 3. The keyword __________ is used to declare a structure variable. Programming Exercises 1. Create a structure date with date, month and year. Display the date. 2. Declare a structure called Employee, which contains the following members: Name Emp_id Department Designation Basic_pay Hra Da Pf Netpay char[20] int char[30] char[30] float float float float float
3. Write a program that reads the above members and calculate the Netpay using function and display all the above.