IDOL Institute of Distance and Online Learning ENHANCE YOUR QUALIFICATION, ADVANCE YOUR CAREER.
BCA 2 All right are reserved with CU-IDOL Computer Programming Course Code: BCA 112 Semester: First SLM Unit: e-Lesson No.: 7 5 www.cuidol.in Unit- 7(BCA112)
Structure 33 OBJECTIVES INTRODUCTION • Describe the basics of structure In this session we are going to learn about • Explain accessing structure members • The basics of structure • Exemplify nested structure • Accessing structure members • Elaborate structure with reference to array, function • Nested structure • Structure with reference to array, function and and pointer pointer www.cuidol.in Unit- 7(BCACA11121)2) INASlTl ITriUgThEt OarFeDrIeSsTeArNvCeEd AwNitDh OCNUL-IIDNOE LLEARNING
TOPICS TO BE COVERED 43 • Basics of Structure INTRODUCTION • Array of Structures • Nested Structure • Structure and Pointers • Structure and Function www.cuidol.in Unit- 7(BCA112) IANlSl TrITigUhTtEaOreFrDeIsSeTrAvNeCdEwAiNthDCOUN-ILDINOELLEARNING
Basics of Structure 5 • A Structure is a collection of logically related different data type items. • Structure is a user-defined datatype which allows us to combine data of different types together. •The syntax for defining a structure is – struct struct_name { DataType member1_name; DataType member2_name; … DataType membern_name; }; • Example – struct book{ int book_id; char book_name[40]; float book_price; }; www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
Basics of Structure 6 • The syntax for declaring a structure variable is – Example – struct struct_name { struct book{ DataType member1_name; int book_id; DataType member2_name; char book_name[40]; … float book_price; } b1, b2; DataType membern_name; OR } var_name; struct book b1, b2; OR struct struct_name var_name; b1.book_id = 1001; b1.book_name = “C Programming”; • Syntax for Accessing and Initializing structure b1.book_price = 567.5; elements – var_name.member1_name; var_name.member2_name; www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
Basics of Structure 7 Example- Output – #include<stdio.h> Name of Student 1: Prakash #include<string.h> Age of Student 1: 28 struct Student { Gender of Student 1: M char name[15]; int age; char gender; }; void main() { struct Student s1; s1.name = \"Prakash\"; s1.age = 28; s1.gender = 'M‘; printf(\"Name of Student 1: %s\\n\", s1.name); printf(\"Age of Student 1: %d\\n\", s1.age); printf(\"Gender of Student 1: %c\\n\", s1.gender); } www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
Basics of Structure 8 • Structure elements are stored in contiguous memory •Output – locations. Address of name = 47518 struct book Address of price = 47519 { Address of pages = 47523 char name ; float price ; b1.name b1.price b1.pages int pages ; }; C 375.00 410 void main( ) 47518 47519 47523 { struct book b1 = { ‘C', 375.00, 410 } ; printf ( \"\\nAddress of name = %u\", &b1.name ) ; printf ( \"\\nAddress of price = %u\", &b1.price ) ; printf ( \"\\nAddress of pages = %u\", &b1.pages ) ; } www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
Basics of Structure 9 Advantages of Structure- 1. C Structure are used for record keeping. Structure act as a database. 2. C Structure can be used to send data to the printer. 3. C Structure can be used o clear output screen contents. www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
Array of Structures 10 • Declaring an array of structure is same as declaring an data[1].id=2; array of fundamental types. strcpy(data[1].name,”Neeraj”); • In an array of structures, each element of an array is of data[1].percentage = 89.5; the structure type. data[2].id=3; Example - strcpy(data[2].name, “Susheela”); struct student { data[2].percentage = 77.5; int id; for(i=0;i<3;i++) char name[20]; { float percentage; }; printf(“Records of STUDENT : %d \\n”, i+1); printf(“ Id is: %d \\n”,data[i].id); void main() { printf(“Name is: %s \\n”, data[i].name); Int i; Printf(“Percentage is: %f\\n\\n”, struct student data[3]; data[i].percentage) data [0].id=1; } strcpy(data [0].name, “Raj”); getch(); data[0].percentage = 68.5; } www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
Array of Structures 11 OUTPUT- Unit- 7(BCA112) All right are reserved with CU-IDOL Record of STUDENT : 1 Id is: 1 Name is : Raj Percentage is : 68.500000 Records of STUDENT : 2 Id is: 2 Name is : Neeraj Percentage is : 89.500000 Record of STUDENT : 3 Id is: 3 Name is : Susheela Percentage is : 77.500000 www.cuidol.in
Nested Structure 12 • Nested Structure means structure within a structure. •One structure can be declared inside other structure as we declare structure members inside a structure. • As the complexity of real world data item increases, it may happen that a structure variable would be defined as a member of another structure. • The declaration of the inner structure must appear before the declaration of the outer structure. www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
Nested Structure 13 Example – void main() printf(\"\\n@@@@@@@@@@\\n\\n\"); #include<stdio.h> { printf(\"Name: %s\\n\", s1.info.name); struct person printf(\"Age: %d\\n\", s1.info.age); { struct student s1; printf(\"DOB: %s\\n\", s1.info.dob); printf(\"Details of student: printf(\"Roll no: %d\\n\", s1.roll_no); char name[20]; \\n\\n\"); printf(\"Marks: %.2f\\n\", s1.marks); int age; printf(\"Enter name: \"); char dob[10]; scanf(\"%s\", s1.info.name); } }; printf(\"Enter age: \"); scanf(\"%d\", &s1.info.age); struct student printf(\"Enter dob: \"); { scanf(\"%s\", s1.info.dob); printf(\"Enter roll no: \"); struct person info; scanf(\"%d\", &s1.roll_no); int roll_no; printf(\"Enter marks: \"); float marks; scanf(\"%f\", &s1.marks); }; www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
Nested Structure 14 Output – Details of student: Enter name: Raj Enter age: 35 Enter dob: 3/4/1985 Enter roll no: 20010 Enter marks: 93 @@@@@@@@@ Name: Raj Age: 35 DOB: 3/4/1985 Roll no: 20010 Marks: 93.00 www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
Structure and Pointers 15 •We can have pointer to a structure. In such case, members are accessed using arrow ( -> ) operator. •Arrow (->) operator is also known as membership operator. Example- Output- #include<stdio.h> 10 20 struct point { int a, b; }; void main() { struct point p1 = {10, 20}; struct Point *p2 = &p1; // p2 is a pointer to structure p1 printf(\"%d %d\", p2->x, p2->y); // Accessing structure members using structure pointer } www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
Structure and Function 16 • A structure can be passed to any function from main void display(struct student s) function or from any sub function using call by value { and call by reference. Example- printf(\"\\nDisplaying Information\\n\"); #include <stdio.h> printf(\"Name: %s\", s.name); struct student { printf(\"\\nAge: %d\", s.age); } char name[50]; int age; }; Input/Output- void display(struct student s); // function prototype Enter name: Param Enter age: 25 void main() { struct student s1; Displaying Information printf(\"Enter name: \"); Name: Param scanf(\"%s\", s1.name); Age: 25 printf(\"Enter age: \"); scanf(\"%d\", &s1.age); display(s1); // passing structure as call by value } www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
Structure and Function 17 • Passing Structure using call by reference. void display(struct student *s) { Example- #include <stdio.h> printf(\"\\nDisplaying Information\\n\"); struct student { printf(\"Name: %s\", s->name); printf(\"\\nAge: %d\", s->age); char name[50]; } int age; }; void display(struct student *s); // function prototype Input/Output- Enter name: Param void main() { Enter age: 25 struct student s1; printf(\"Enter name: \"); Displaying Information scanf(\"%s\", s1.name); Name: Param printf(\"Enter age: \"); Age: 25 scanf(\"%d\", &s1.age); display(&s1); //passing structure as call by All right are reserved with CU-IDOL reference } www.cuidol.in Unit- 7(BCA112)
Multiple Choice Questions 18 1. Can we declare function inside structure of C Programming? a) Yes b) No c) Depends on Compiler d) Yes but run time error 2. Find the output of below code - Unit- 7(BCA112) All right are reserved with CU-IDOL #include <stdio.h> struct abc{ float f; char c [10]; }; void main() { printf(\"%d\",sizeof(struct abc)); } a) 5 b) 7 c) 10 d) 14 Answers: 1. b) 2.d) www.cuidol.in
Multiple Choice Questions 19 3. What is the output of the C code? 4. What is the output of the C code? #include <stdio.h> #include<stdio.h> struct Point void main() { { int x,y,z }; struct student int main() { { struct Point p1={,y=0, .z=1, .x=2}; int no; printf(“%d%d”, p1.x, p1.y, p1.z); char name[20]; return 0; }; } struct student s; a) Compile Error s.no = 8; b) 012 printf(“%d”,s.no); c) 2 0 1 } d) 2 1 0 a) 8 b) Compile time error Unit- 7(BCA112) All right are reserved with CU-IDOL c) Junk d) Nothing Answers: 3. a) 4.c) www.cuidol.in
SUMMARY 20 Let us recapitulate the important concepts discussed in this session: • A Structure is a collection of logically related different data type items. •Structure is a user-defined datatype which allows us to combine data of different types together. •Structure elements are stored in contiguous memory locations. •There are many advantages of Structures- C Structure are used for record keeping. Structure act as a database. C Structure can be used to send data to the printer. C Structure can be used o clear output screen contents. •Declaring an array of structure is same as declaring an array of fundamental types. •In an array of structures, each element of an array is of the structure type. •Nested Structure means structure within a structure. •The declaration of the inner structure must appear before the declaration of the outer structure. •We can have pointer to a structure. In such case, members are accessed using arrow ( -> ) operator. •Arrow (->) operator is also known as membership operator. •A structure can be passed to any function from main function or from any sub function using call by value and call by reference. www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
FREQUENTLY ASKED QUESTIONS 21 Q1. What is meant by structure. List out its advantages. Ans. A Structure is a collection of logically related different data type items. For further details please refer to the slide no. 10 and subject SLM unit 7. Q2. Differentiate between array and structure. Ans: A Structure is a collection of logically related different data type items. Where array is collection of same data type items. For further details please refer to the subject SLM unit 7. Q3. Describe passing structure variable to function. Ans: A structure can be passed to any function from main function or from any sub function using call by value and call by reference. For further details please refer to slide no. 17 -18 and subject SLM Unit 7. Q4: Exemplify nested structure. Ans: A Null Pointer is a pointer that does not point to any memory location. It has value NULL. For further details please refer to slide no. 13 -14 and the subject SLM unit 7. www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
REFERENCES 22 • CU-IDOL’s “Computer Programming” SLM • Balaguruswamy (2017).Programming in ANSI C. New Delhi: McGraw-Hill. • Kanetkar, Y. (2014), Programming in C ANSI standard.New Delhi: BPB Publications. • Gottfried (2005).Programming with C. New York: Tata McGraw Hill. • Harrow K., Jones J. (1996).Problem Solving with C. London: Pearson Education. • Jeri R., Hanly,KoffmanE.P. (2000).Problem Solving and Program Design in C. 3rd Ed. Boston: Addison Wesley. www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
23 THANK YOU www.cuidol.in Unit- 7(BCA112) All right are reserved with CU-IDOL
Search
Read the Text Version
- 1 - 23
Pages: