View
216
Download
0
Category
Tags:
Preview:
Citation preview
CS 202Computer Science II Lab
Fall 2009October 15
Data Types
• Structure• Typedef• Union• Enum
Data Types
• Structurestruct product {
int weight; float price;
} apple;
apple.price
Data Types
• Structurestruct product {
int weight; float price;
} apple, banana, melon;
apple.price
banana.weight
Data Types
• Structurestruct product {
int weight; float price;
} fruit[10];
fruit[5].price
fruit[5].weight
Data Types
• Pointer to Structure
*(apple.weight)Value pointed by member weight of object apple
*apple.weight
(*apple).weightMember weight of object pointed by apple
apple->weight
Member weight of object appleapple.weight
Equivalent What is evaluated Expression
Data Types
• Nesting Structurestruct movies_t {
string title; int year;
}; struct friends_t {
string name;string email; movies_t favorite_movie;
} charlie, maria;
friends_t * pfriends = &charlie;
charlie.name maria.favorite_movie.title charlie.favorite_movie.year
pfriends->favorite_movie.year
Data Types
• Typedef – Defined data types
typedef char C;
typedef unsigned int WORD;
typedef char * pChar;
typedef char field [50];
C mychar, anotherchar, *ptc1;
WORD myword;
pChar ptc2;
field name;
Data Types
• Union– allows same portion of memory to be accessed as different
data types
union myTest {
char c;
int i;
float f;
} myVar;
myVar.c
myVar.i
myVar.f
Data Types
• Union
union mix_t {
long l;
struct {
short hi;
short lo;
} s;
char c[4];
} mix;
0xA70x430x510x12mix
mix.l
mix.s.hi mix.s.lo
mix.c[0] mix.c[1] mix.c[2] mix.c[3]
Comparison Between Struct and Union in terms of Memory Space
#include <iostream>using namespace std;struct myStruct{
bool a;bool c;char b[4];
} A;int main(){
cout<<sizeof(A)<<'\n';return 0;
}
Answer:>>6
#include <iostream>using namespace std;union myUnion{
bool a;bool c;char b[4];
} A;int main(){
cout<<sizeof(A)<<'\n';return 0;
}
Answer:>>4
Data Types
• Anonymous Union
struct { char title[50]; char author[50]; union { float dollars; int yens; };
} book;
struct { char title[50]; char author[50]; union { float dollars; int yens; } price;
} book;
structure with anonymous union structure with regular union
book.price.dollars
book.price.yens
book.dollars
book.yens
Data Types
• Enum– Enumerations create new data types
enum colors_t {black, blue, green, cyan, red, purple, yellow, white};
colors_t mycolor;
…
mycolor = blue;
if (mycolor == green) mycolor = red;
enum months_t { january, february, march, april, may, june, july, august, september, october, november, december
} myMonth;
enum months_t { january=1, february, march, april, may, june, july, august, september, october, november, december
} myMonth;
myMonth = January; /* myMonth=0 */
myMonth = february; /* myMonth=1 */
myMonth = march; /* myMonth=2 */
myMonth = January; /* myMonth=1 */
myMonth = february; /* myMonth=2 */
myMonth = march; /* myMonth=3 */
Questions?
Data Types#include <iostream>
using namespace std;
struct myStruct{
bool a;
bool c;
char b[4];
} A;
int main(){
cout<<sizeof(A)<<'\n';
return 0;
}
Recommended