15
CS 202 Computer Science II Lab Fall 2009 October 15

CS 202 Computer Science II Lab Fall 2009 October 15

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 202 Computer Science II Lab Fall 2009 October 15

CS 202Computer Science II Lab

Fall 2009October 15

Page 2: CS 202 Computer Science II Lab Fall 2009 October 15

Data Types

• Structure• Typedef• Union• Enum

Page 3: CS 202 Computer Science II Lab Fall 2009 October 15

Data Types

• Structurestruct product {

int weight; float price;

} apple;

apple.price

Page 4: CS 202 Computer Science II Lab Fall 2009 October 15

Data Types

• Structurestruct product {

int weight; float price;

} apple, banana, melon;

apple.price

banana.weight

Page 5: CS 202 Computer Science II Lab Fall 2009 October 15

Data Types

• Structurestruct product {

int weight; float price;

} fruit[10];

fruit[5].price

fruit[5].weight

Page 6: CS 202 Computer Science II Lab Fall 2009 October 15

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

Page 7: CS 202 Computer Science II Lab Fall 2009 October 15

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

Page 8: CS 202 Computer Science II Lab Fall 2009 October 15

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;

Page 9: CS 202 Computer Science II Lab Fall 2009 October 15

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

Page 10: CS 202 Computer Science II Lab Fall 2009 October 15

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]

Page 11: CS 202 Computer Science II Lab Fall 2009 October 15

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

Page 12: CS 202 Computer Science II Lab Fall 2009 October 15

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

Page 13: CS 202 Computer Science II Lab Fall 2009 October 15

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 */

Page 14: CS 202 Computer Science II Lab Fall 2009 October 15

Questions?

Page 15: CS 202 Computer Science II Lab Fall 2009 October 15

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;

}