1 / 8

Structures

Structures. putting data together. Structure Definitions. aggregate construct allows to manipulate several data items as a single whole structures are an aggregate construct; what other aggregate constructs have we studied? example: a particular date has month, day and year

edon
Download Presentation

Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structures putting data together

  2. Structure Definitions • aggregate construct allows to manipulate several data items as a single whole • structures are an aggregate construct; • what other aggregate constructs have we studied? • example: a particular date has month, day and year • structure definition: struct Date{ int month; int day; int year; }; • note the semicolon at the end of the structure definition • elements of the structure are called members or member variables • structure definition is not executable – good candidate for a header file • members of the same structure must have different names, different structures can contain members with the same name • that is: the scope of member variable name is the structure definition

  3. Preprocessor Directives (Review) • each definition (e.g. global constant def.) can be encountered only once during compilation • when definition is placed in a header file, it may be included multiple times • header file must structured so it is safe in case of multiple inclusion; term – multiple inclusion protection • mechanism - preprocessor directives #define name value note that substitution is textual • problem: #define press 50+5 int myvar = press * 20; changes order of operations, do not use #define instead of global constants #ifdef name - true if name defined, #ifndefname - true if not #endif - completes #if • header file myheader.h containing definitions usually has the following structure: #ifndef MYHEADER_H #define MYHEADER_H // text of the header file goes here #endif

  4. Structure Variables • defined structure Date is a type • part of the language types such as int, char are built-in or basic • a structure variables can be declared as built-in type variable : Date today, birthday; • each structure variable of Date contains three member variables • programmer can refer to member variable by specifying structure variable name “dot” member variable name • for example today contains: today.month today.day today.year • member variables are of type specified in the structure definition • a member variable can be used as any other (scalar) variabletoday.year=2001; today.day = birthday.day+1; cin >> today.month;

  5. Assigning Value to Structure Variable • initialization • structure variable can be initialized at declaration: Date birthday={10, 31, 2035}; • order of initialization corresponds to the order member of member variables in structure definition • more values than variable members – error • less values than variable members – the rest initialized to zero • structure variable can be assigned the value of another structure variable of the same structure type Date deadline; deadline=today; • structure variables cannot be directly compared if (deadline == today) // error how would you compare two structure variables?

  6. Passing Structures as Parameters,Returning Structures structures can be passed by value and by reference, a function can return a structure: Date setDate(int m, int d, int y){ Date tmp; tmp.month=m; tmp.day=d; tmp.year=y; return tmp; } what does this code do? Date projectDue; projectDue=setDate(10,31,2035);

  7. Complex Structures • a member may be of basic type or of type structure • substructure – member variable of type structure // example structure struct Example{ int a; string b; }; // example complex structure struct CompExample{ int c; Example d; // substructure }; • if there is a declaration compExample me;how do you access substructure member variables?

  8. Structures and Arrays • a member may be an array struct ExampleWArray{ // complex structure definition int a; int b[5]; // member array }; ExampleWArray se; // declaring structure variable se.a = 123; se.b[3]=456; // accessing members • an array of structures may be declared struct Example{ //structure int a; string b; }; Example as[4]; // declaring array of structures as[3].a=123; as[3].b=”Hello”; // accessing members how do you access characters of string b? • more complicated constructs (arrays of structures with substructures, etc.) are possible

More Related