unions and typedefstbarten1/cs211_fall_2015/lectures/l25 - … · why typedef 1. makes code much...
TRANSCRIPT
![Page 1: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/1.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Unions and Typedefs
1
![Page 2: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/2.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Symbol Table
• A symbol table consists of several pieces of information
2
NAME TYPE VALUE
X INT 13
Y INT 0
DIST FLOAT 12.87
FIRST_INIT CHAR ‘T’
SSN LONG INT 222 22 2222
![Page 3: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/3.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Symbol Table Structure
struct symbol {
char name[256];
enum typ { t_int,t_float,t_char,t_long_int} type;
?????? value;
} symbolTable[100];
3
What type of data should value be?
![Page 4: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/4.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Symbol Table Structure
struct symbol {
char name[256];
enum typ { t_int,t_float,t_char,t_long_int} type;
int intValue;
float floatValue;
char charValue;
long int liValue;
} symbolTable[100];
4
Why reserve space for each of these?We will only use one value for each symbol!
![Page 5: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/5.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Example Union
union val_union {
int i;
float f;
char c;
long int li;
};
5
![Page 6: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/6.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Anatomy of a Union
union val_union {
int i;
float f;
char c;
long int li;
};
• Like structures or enums, this defines a new data type
• Like structures, contains a list of sub-fields
• Unlike structures, all sub-fields occupy the same memory
6
Type Name
Members or Fields
![Page 7: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/7.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Instance of a union
union val_union value;
• Referencing a union instance:value.i=13;
or
value.f=12.87;
• Like structures – reference fields using union name . field
• Like structures, field names are NOT variable names
7
Type Name
Variable… Instance of val_union
![Page 8: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/8.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Value.c
Value.li
Value.f
Union Instances in Memory
• All fields start at the same place
• Entire Union is large enough to hold the largest field
8
Value.i
![Page 9: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/9.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
xmp.b[3]xmp.b[2]xmp.b[1]xmp.b[0]
Using Unions to Look at Internals
union { int i;
char b[4];
} xmp;
xmp.i=347;
printf(“347 is represented as %2x %2x %2x %2x\n”,
xmp.b[0],xmp.b[1],xmp.b[2],xmp.b[3]);
347 is represented as 5B 01 00 00
9
xmp.i
![Page 10: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/10.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Pointers to Unions
• Like structures, we can use -> pointer notation to access fields
union val_union * vptr;
vptr=(union val_union*)malloc(sizeof(union val_union));
vptr->c=‘T’;
…
if (vptr->c==‘X’) printf(“Hi Xavier”);
free(vptr);
10
![Page 11: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/11.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Symbol Table Structure
struct symbol {
char name[256];
enum typ { t_int,t_float,t_char,t_long_int} type;
union val_union value;
} st[100];
…
if (st[i].type==t_float)
printf(“%s=%f\n”,st[i].name, st[i].value.f);
11
![Page 12: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/12.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Defining New Types
• Built in types: char, int, float, short int, long int, double, etc.
• Arrays – extended types
• Pointers – extended types
• Derived types: structures, enums, unions• Must include “struct lnode” or “union val_union”
12
![Page 13: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/13.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Derived Types can get Complicated
• struct lnode* nodeList[10];• nodelist is an array of 10 pointers to lnode structures
• union val_union **vptrs;• vptrs is a pointer to one or more pointers to one or more instances of a
val_union union
13
![Page 14: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/14.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
TypeDef… Synonym for Any Type
typedef struct lnode * nodeptr;
nodeptr head;
nodeptr newNode=makeLnode(12);
14
![Page 15: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/15.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Anatomy of a Typedef
typedef struct lnode * nodeptr;
15
Built-In or DerivedType Name
New Type Name(Synonym)
![Page 16: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/16.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Why TypeDef
1. Makes code much more readable• It’s much clearer to read an write “nodeptr” than “struct lnode *”
2. Improves portability of code
typedef float length_t;
length_t aToB;
length_t bToC;
…
16
Make this “double” to change all lengths!
![Page 17: Unions and Typedefstbarten1/CS211_Fall_2015/lectures/L25 - … · Why TypeDef 1. Makes code much more readable •It’s much clearer to read an write “nodeptr” than “struct](https://reader034.vdocuments.us/reader034/viewer/2022050611/5fb20108e7870746786182f8/html5/thumbnails/17.jpg)
Binghamton
University
CS-211
Fall 2015
Binghamton
University
CS-211
Fall 2015
Resources
• Programming in C, Chapter 16 (Working with Unions)
• Wikipedia Union Type https://en.wikipedia.org/wiki/Union_type
• Wikipedia Typedef https://en.wikipedia.org/wiki/Typedef
17