Presentation is loading. Please wait.

Presentation is loading. Please wait.

CGS 3460 Thus Far n Whenever we declare a variable, we specified its data type. n The data type helped us identify the type of information that a variable.

Similar presentations


Presentation on theme: "CGS 3460 Thus Far n Whenever we declare a variable, we specified its data type. n The data type helped us identify the type of information that a variable."— Presentation transcript:

1 CGS 3460 Thus Far n Whenever we declare a variable, we specified its data type. n The data type helped us identify the type of information that a variable holds. n So far we have seen simple data types lint - represent whole numbers lfloat - represent numbers with decimal (fractional part), lchar - represent characters n We have also seen arrays, that allow us to represent a collection of data elements of a specific type with a single variable name. n We accessed individual elements in the collection using the array subscript operator [].

2 CGS 3460 Structures n Structures in C allows us to represent a collection of variables (possibly of different types) under a single name. n It allows us to create record style data with various fields. n More generally, it allows us to model real-world entities. n An entity is something that has a distinct, seperate existence. n An entity has a set of attributes that describes it. n Any object in the real-world can be modeled as an entity. n A book is an entity that can be distinguished from say a car.

3 CGS 3460 Examples Book Title Author Price Car Make Model Year Student Name ID Age Major Hometown Country Name Capital Population Longitude Latitude

4 CGS 3460 Examples – Programming Specific Book string Title string Author float Price Car int Make int Model int Year Student string Name string ID unsigned Age int Major string Hometown Country string Name string Capital unsigned Population float Longitude float Latitude

5 CGS 3460 Structure – Declaring n A collection of values (members) – type declaration struct struct_name { type1 data_member1; type2 data_member2; …; typeN data_memberN; }; n Declare a structure variable – instance declaration lstruct struct_name instance_name;

6 CGS 3460 Examples – Programming Specific Student string Name string ID unsigned Age int Major stringHometown struct Student { char Name[50]; char ID[9]; unsigned Age; int Major; char Hometown }; struct Student you;

7 CGS 3460 Examples – Programming Specific Book string Title string Author float Price struct Book { char Title[100]; char Author[100]; float price; }; struct Book bestSeller;

8 CGS 3460 Examples – Programming Specific Car int Make int Model int Year struct Car { int Make; int Model; int Year; }; struct Car carForSale;

9 CGS 3460 Examples – Programming Specific Country string Name string Capital unsigned Population float Longitude float Latitude struct Country { char Name[100]; char Capital[100]; unsigned Population; float Longitude; float Latitude; }; struct Country home;

10 CGS 3460 Examples – Programming Specific Date int Day string Month int Year struct Date { int Day; char Month[10]; int Year; }; struct Date today;

11 CGS 3460 Declarations n Three ways struct date{ int day; char month[10]; int year; }; struct date today; typedef struct { int day; char month[10]; int year; } date; date today; struct { int day; char month[10]; int year; } today;

12 CGS 3460 Initialization struct { int day; char month[10]; int year; } today = {15, “ June ”, 2007}; typedef struct { int day; char month[10]; int year; } date; date today = {15, “June”, 2007}; struct date{ int day; char month[10]; int year; }; struct date today = {15, “June”, 2007};

13 CGS 3460 How to use n To access the members in the structure lspecify the variable name, followed by a period and the member name today.day = 15; today.year = 2007; today.month[0]=‘J’; today.month[1]=‘u’; today.month[2]=‘n’; today.month[3]=‘e’; today.month[4]=‘\0’; lOR today.day = 15; today.year = 2007; today.month=“June”; 15 ‘J’ ‘u’ ‘n’ ‘e’ ‘\0’ 2007.month.day.year today

14 CGS 3460 How to use structure – cont. n Structure variable can be passed as a parameter to a function. n Structure variable can be returned from a function n Can have arrays of structures, and structures containing arrays or other structures. lstruct date list_of_days[10]; lstruct journalEntry { int location; struct date when; char entry[1000]; };

15 CGS 3460 Scope n A structure type declaration can be local or global lif declared locally, it is only valid locally. struct ONE { int a; float b; }; void main () { struct ONE x; } int f() { struct ONE x; } void main () { struct ONE { int a; float b; }; struct ONE x; } int f() { struct ONE x; }


Download ppt "CGS 3460 Thus Far n Whenever we declare a variable, we specified its data type. n The data type helped us identify the type of information that a variable."

Similar presentations


Ads by Google