# Ocs752 unit 5

22 de Jun de 2020
1 de 7

### Ocs752 unit 5

• 1. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 1 DHANALAKSHMI COLLEGE OF ENGINEERING Tambaram, Chennai Department of Computer Science and Engineering OCS752 INTRODUCTION TO C PROGRAMMING Year / Sem : IV / VII 2 Marks Q & A
• 2. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 2 UNIT V STRUCTURES Introduction to structures – Declaration – Initialization – Accessing the members – Nested Structures – Array of Structures – Structures and functions – Passing an entire structure – Exercise programs − Compute the age of a person using structure and functions (passing a structure to a function) – Compute the number of days an employee came late to the office by considering his arrival time for 30 days (Use array of structures and functions) PART – A 1. Define – Structure (N/D – 17) Structure is defined as a collection of different data types which are grouped together and each element in a C structure is called member. Structure variable should be declared to access structure members in C. Keyword struct is used to define the structure. 2. Create a structure called ID_Card to hold the details of a student. (N/D – 15) Structure to hold the details of a student struct ID_Card { char Name[50]; int roll; int year; char dept[50]; int phonenumber; }; 3. What are the uses of structure? (N/D – 16)(N/D – 19) Uses of structure 1) Structure is used to store different data types. 2) Each element can be accessed individually. 4. Write the syntax of structure. The keyword struct is used to create a structure. Syntax: struct structname { datatype member 1; datatype member 2; -------------
• 3. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 3 datatype member m; }; 5. Specify the use of typedef. typedef is used to create a new data using the existing type. Syntax: typedef datatype name; Example: typedef int hours; hours hrs; 6. Write the syntax for initialize a structure variable. struct structname { datatype member 1; datatype member 2; ------------- datatype member m; }structvar={constant1, constant2, …..}; 7. Differentiate array from structure. (A/M – 19) S. No. Array Structure 1 Array is a collection of data items of same data. Structure is a collection of data items of different type. 2 Array name represents the address of the starting element. Structure name is known as tag. 3 Array cannot have bit fields. Structure may contain bit fields 8. List the features of structure. (A/M – 15) Features of structure 1) All the elements of a structure are stored at contiguous memory locations 2) A variable of structure type can store multiple data items of different data types under the one name. 9. How will you access the structures member through pointers? The structures member can be accessed through pointers by the following ways 1) Referencing pointer to another address to access memory 2) Using dynamic memory allocation
• 4. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 4 10. What is structure within structure? Nested structure is structure within structure. One structure can be declared inside other structure as declaring structure members inside a structure. The structure variables can be a normal structure variable or a pointer variable to access the data. Example struct complex { int imagvalue; float realvalue; }; struct number { struct complex comp; int real; } num1, num2; 11. Write a C program to read and display information of a student using a structure within a structure. #include <stdio.h> struct student{ char name[30]; int rollNo; struct dateOfBirth{ int dd; int mm; int yy; }DOB; }; void main() { struct student std; gets(std.name); scanf("%d",&std.rollNo); scanf("%d%d%d",&std.DOB.dd,&std.DOB.mm,&std.DOB.yy); printf(" %s %d %d/%d/%dn",std.name,std.rollNo,std.DOB.dd,std.DOB.mm,std.DOB.yy); } 12. How can a structure member be processed? How is a structure member accessed? The members of a structure are usually processed individually as separate entities. A structure member can be accessed by writing variable member name.
• 5. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 5 13. Write a C program using structures to find the biggest of three numbers. #include <stdio.h> int main() { struct numbers { int a,b,c, largest; } n; scanf("%d %d %d", &n.a, &n.b, &n.c); n.largest = n.a > n.b ? (n.a > n.c? n.a :n.c) : (n.b > n.c ? n.b :n.c); printf("%d is the largest number.",n.largest); } 14. What is the precedence of the period operator? What is its associativity? This period (.) is an operator. It is a member of the highest precedence group, and its associativity is left-to-right. The use of the period operator can be extended to arrays of structure by writing array[expression]. member. 15. What is self-referential structure? A self-referential structure is one of the data structures which refer to the pointer to points to another structure of the same type. Example: struct node { int value; struct node *next; }; 16. Write a C program for passing a structure to a function. #include<stdio.h> struct p { int x,y; }point; void display(point); void main() { point p1={2,3}; display(p1); }
• 6. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 6 void display(point p1) { printf(“%d %d”,p1.x,p1.y); } 17. What is array of structure? An array of structure in C is the collection of multiple structures variables, where each variable contains information about different entities. The arrays of structures in C are used to store information about multiple entities of different data types. 18. What are the two ways of passing a structure to function in C? Two ways of passing a structure to function in c 1) Passing structure to a function by value 2) Passing structure to a function by address(reference) 19. Declare a structure to create an inventory method. struct inventory { char productname; float price; int stock; }; 20. Write a C program for passing structures through pointers. #include <stdio.h> struct student { int id; char name[30]; }; void main() { int i; struct student record1 = {1, “Raju”}; struct student *ptr; ptr = &record1; printf(“ Id is: %d n”, ptr−>id); printf(“ Name is: %s n”, ptr−>name); }
• 7. OCS752 − INTRODUCTION TO C PROGRAMMING VII SemesterEEE Dept. of CSE Dhanalakshmi College of Engineering 7 21. Write a C program using typedef keyword. #include<stdio.h> struct Point1{ int x; int y; }; typedef struct Point1 Point; void main() { Point p1; p1.x = 1; p1.y = 3; printf("%d %d", p1.x, p1.y); }