Docsity

Programming fundmental, Slides of C programming

Partial preview of the text

Download Programming fundmental and more Slides C programming in PDF only on Docsity!

Programming

Fundamentals

Course Instructor: Engr. Asma Sattar Lab Instructor: Sir. Khalid ;

Today’s Lecture

 Passing Structure in a function  Nested Structures  Arrays of Structure  Enumerations  Introduction to pointers

Nested Structures

Initializing Nested Structures

 How to initialize a structure variable that itself contains structures?  Each structure of type Distance, which is embedded in Room, is initialized separately.  Remember that this involves surrounding the values with braces and separating them with commas.

Task

Struct Measure { float pounds; int kg; float inch; } Struct Person { Measure height; Measure Weight; }

main() { Person c1;Person p1; c1.weight.pounds=4; p1.weight.kg=80; c1.height.inch=3; p1. height.inch=5.5; } main() { Person c1={{0,0,3}, {4,0,0}}; Person p2 ={{0,0,5.5} ,{0,80,0}}; }

Output can be like this: Main Menu Press 1 to enter student record. Press 2 to display record. Press 3 to exit. Enter your choice:____ Note: if user press 1 then your program should display this: Press 1 to enter Student 1 record. Press 2 to enter Student 2 record. Press 3 to enter Student 3 record. Press 4 to return to main menu. Enter your choice:______

 If user press 2 form main menu record should

be displayed like this:

 An enumeration is a list of all possible values.  This is unlike the specification of an int, for example, which is given in terms of a range of values.  In an enum you must give a specific name to every possible value.

 Enumerations are treated internally as integers.  This explains why you can perform arithmetic and relational operations on them.  Ordinarily the first name in the list is given the value 0, the next name is given the value 1, and so on.  In previous example, the values Sun through Sat are stored as the integer values 0–6.