#include <stdio.h> // for printf()
typedef int* IntPtr;
int main()
{
int* a = 0, b = 0; // a is a null pointer, b is an integer with value 0
a++, b++; // a points to address 4 (sizeof int), b == 1
printf("a = %p, b = %p\n", a, b); // warning: b is not pointer
printf("a = %d, b = %d\n", a, b); // warning: a is not integer
IntPtr x = 0, y = 0; // null pointers
printf("x = %p, y = %p\n", x, y); // nil
x = y = a = &b; // now they point to something
*x = 0; // b = 0
printf("a = %p, &b = %p, x = %p, y = %p\n", a, &b, x, y);
printf("*a = %d, b = %d, *x = %d, *y = %d\n", *a, b, *x, *y);
return 0;
}
/*
gcc typedef.c -o typedef
./typedef
a = 0x4, b = 0x1
a = 4, b = 1
x = (nil), y = (nil)
a = 0x7ffdc61ccddc, &b = 0x7ffdc61ccddc, x = 0x7ffdc61ccddc, y = 0x7ffdc61ccddc
*a = 0, b = 0, *x = 0, *y = 0
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
typedef int* IntPtr;
int main()
{
int* a = 0, b = 0; // a is a null pointer, b is an integer with value 0
a++, b++; // a points to address 4 (sizeof int), b == 1
cout << "a = " << a << ", b = " << b << endl;
IntPtr x = nullptr, y = nullptr; // null pointers
cout << "x = " << x << ", y = " << y << endl; // 0
x = y = a = &b; // now they point to something
*x = 0; // b = 0
cout << "a = " << a << ", &b = " << &b
<< ", x = " << x << ", y = " << y << endl;
cout << "*a = " << *a << ", b = " << b
<< ", *x = " << *x << ", *y = " << *y << endl;
return 0;
}
/*
g++ Typedef.cpp -o Typedef
./Typedef
a = 0x4, b = 1
x = 0, y = 0
a = 0x7ffef1be638c, &b = 0x7ffef1be638c, x = 0x7ffef1be638c, y = 0x7ffef1be638c
*a = 0, b = 0, *x = 0, *y = 0
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
struct Structure1 // type name `struct Structure1' defined in C
{
char c;
int i;
float f;
double d;
};
// In C, the type name is `struct Structure1', so `Structure1' is considered
// here a parameter (variable) name:
// void print(Structure1); // warning: param names (without types) in func decl
// void print(Structure1 s); // compile error: unknown type name `Structure1'
void printStructure1(struct Structure1); // C requires `struct Structure1'
void printStructure1(struct Structure1 s); // alternative declaration
int main()
{
// Structure1 Structure1; // compile error: unknown type name `Structure1'
struct Structure1 Structure1; // `Structure1' becomes a variable name
struct Structure1 s1, s2;
// struct Structure1 s3, struct Structure1 s4; // compile error
struct Structure1 s3; struct Structure1 s4; // OK
printStructure1(Structure1); // uninitialized variables
printStructure1(s1); // of type `struct Structure1'
Structure1.c = 'a'; // Select an element using a '.'
Structure1.i = 1;
Structure1.f = 3.14;
Structure1.d = 0.00093;
s1.c = 'a';
s1.i = 1;
s1.f = 3.14;
s1.d = 0.00093;
printStructure1(Structure1); // initialized variables
printStructure1(s1); // of type `struct Structure1'
}
void printStructure1(struct Structure1 s)
{
printf("c = %c (%d), i = %d, f = %g, d = %g\n", s.c, s.c, s.i, s.f, s.d);
}
/*
gcc simplestruct1.c -o simplestruct1
./simplestruct1
c = F (70), i = 32765, f = -1.65213e+30, d = 6.91284e-310 // garbage
c = (0), i = 0, f = -1.65205e+30, d = 6.9529e-310 // values
c = a (97), i = 1, f = 3.14, d = 0.00093
c = a (97), i = 1, f = 3.14, d = 0.00093
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
struct Structure1 // type names `Structure1',
{ // `struct Structure1' defined in C++
char c;
int i;
float f;
double d;
};
void printStructure1(struct Structure1 s); // C requires `struct Structure1'
void printStructure1(Structure1 s); // alternative declaration in C++
int main()
{
struct Structure1 s1, s2; // C style: `struct Structure1'
Structure1 s3, s4; // OK in C++, C requires `struct Structure1'
// struct Structure1 s5, Structure1 s6; // compile error
struct Structure1 s5; Structure1 s6; // OK in C++
// Structure1 s7, Structure1 s8; // compile error
Structure1 s7; Structure1 s8; // OK in C++
Structure1 Structure1; // type name `Structure1' becomes variable name
// struct Structure1 Structure1; // same effect
// From now on we must use `struct Structure1' for the type name (as in C):
// Structure1 s9; // compile error: `Structure1' is no longer a type name
struct Structure1 s9; // OK (as in C), `struct Structure1' still a type name
printStructure1(Structure1); // uninitialized variables
printStructure1(s1); // of type `struct Structure1'
Structure1.c = 'a'; // Select an element using a '.'
Structure1.i = 1;
Structure1.f = 3.14;
Structure1.d = 0.00093;
s1.c = 'a';
s1.i = 1;
s1.f = 3.14;
s1.d = 0.00093;
printStructure1(Structure1); // initialized variables
printStructure1(s1); // of type `struct Structure1'
}
void printStructure1(Structure1 s) // C requires `struct Structure1'
{
cout << "c = " << s.c << " (" << int(s.c) << "), i = " << s.i
<< ", f = " << s.f << ", d = " << s.d << endl;
}
/*
g++ SimpleStruct1.cpp -o SimpleStruct1
./SimpleStruct1
c = (0), i = 0, f = 1.09299e-32, d = 6.95265e-310 // garbage
c = (2), i = 0, f = 1.09305e-32, d = 6.92712e-310 // values
c = a (97), i = 1, f = 3.14, d = 0.00093
c = a (97), i = 1, f = 3.14, d = 0.00093
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
// Using typedef with struct
typedef struct
{
char c;
int i;
float f;
double d;
} Structure2; // type name `Structure2' defined
// void printStructure2(struct Structure2 s); // warning (`struct Structure2'
// type name defined in the parameter list, not visible outside of it),
// compile errors later
void printStructure2(Structure2 s); // OK
void printStructure2(Structure2); // alternative declaration
int main()
{
Structure2 s1;
Structure2 Structure2; // type name `Structure2' becomes a variable name
// struct Structure2 s2; // compile error: storage size of `s2' isn't known
// Structure2 s2; // compile error: `Structure2' is no longer a type name
struct Structure2{} s2; // OK, `struct Structure2' type name defined
struct Structure2 s3;
// Structure2 s4; // compile error: `Structure2' is no longer a type name
s1.c = 'a';
s1.i = 1;
s1.f = 3.14;
s1.d = 0.00093;
Structure2.c = 'a'; // `Structure2' can still be used as a variable name
Structure2.i = 1;
Structure2.f = 3.14;
Structure2.d = 0.00093;
printStructure2(s1);
printStructure2(Structure2);
return 0;
}
void printStructure2(Structure2 s)
{
printf("c = %c (%d), i = %d, f = %g, d = %g\n", s.c, s.c, s.i, s.f, s.d);
}
/*
gcc simplestruct2.c -o simplestruct2
./simplestruct2
c = a (97), i = 1, f = 3.14, d = 0.00093
c = a (97), i = 1, f = 3.14, d = 0.00093
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
// Using typedef with struct
typedef struct
{
char c;
int i;
float f;
double d;
} Structure2; // type name `Structure2' defined
// void printStructure2(struct Structure2 s); // compile error:
// `struct Structure2' not a type name
void printStructure2(Structure2 s); // OK
void printStructure2(Structure2); // alternative declaration
int main()
{
Structure2 s1;
Structure2 Structure2; // type name `Structure2' becomes a variable name
// struct Structure2 s2; // compile error: `struct Structure2' not a type name
// Structure2 s2; // compile error: `Structure2' is no longer a type name
struct Structure2{} s2; // OK, `struct Structure2' type name defined
struct Structure2 s3;
// Structure2 s4; // compile error: `Structure2' is no longer a type name
s1.c = 'a';
s1.i = 1;
s1.f = 3.14;
s1.d = 0.00093;
Structure2.c = 'a'; // `Structure2' can still be used as a variable name
Structure2.i = 1;
Structure2.f = 3.14;
Structure2.d = 0.00093;
printStructure2(s1);
printStructure2(Structure2);
return 0;
}
void printStructure2(Structure2 s)
{
cout << "c = " << s.c << " (" << int(s.c) << "), i = " << s.i
<< ", f = " << s.f << ", d = " << s.d << endl;
}
/*
g++ SimpleStruct2.cpp -o SimpleStruct2
./SimpleStruct2
c = a (97), i = 1, f = 3.14, d = 0.00093
c = a (97), i = 1, f = 3.14, d = 0.00093
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
// Allowing a struct to refer to itself
typedef struct SelfReferential // type name `struct SelfReferential' defined in C
{
int i;
// SelfReferential* sr; // error: type name `SelfReferential' not yet defined
struct SelfReferential* sr; // OK
} SelfReferential; // defines type name `SelfReferential' in C
int main()
{
struct SelfReferential sr1, sr2; // SelfReferential sr1, sr2;
sr1.sr = &sr2;
sr2.sr = &sr1;
sr1.i = 47;
sr2.i = 1024;
printf("sr1.i = %d\n", sr1.i); // 47
printf("(sr2.sr)->i = %d\n", (sr2.sr)->i); // 47
printf("sr2.i = %d\n", sr2.i); // 1024
printf("(sr2.sr)->sr->i = %d\n", (sr2.sr)->sr->i); // 1024
printf("(sr1.sr)->sr->sr->i = %d\n", (sr1.sr)->sr->sr->i); // 1024
SelfReferential SelfReferential; // `SelfReferential' becomes a variable name
// SelfReferential sr3; // `SelfReferential' is no longer a type name
struct SelfReferential sr3; // `struct SelfReferential' is still a type name
SelfReferential.sr = &SelfReferential; // truly SelfReferential
SelfReferential.i = 25;
printf("SelfReferential.i = %d\n", SelfReferential.i); // 25
printf("(SelfReferential.sr)->i = %d\n", (SelfReferential.sr)->i); // 25
printf("(SelfReferential.sr)->sr->i = %d\n",
(SelfReferential.sr)->sr->i); // 25
printf("(SelfReferential.sr)->sr->sr->i = %d\n",
(SelfReferential.sr)->sr->sr->i); // 25
return 0;
}
/*
gcc selfreferential.c -o selfreferential
./selfreferential
sr1.i = 47
(sr2.sr)->i = 47
sr2.i = 1024
(sr2.sr)->sr->i = 1024
(sr1.sr)->sr->sr->i = 1024
SelfReferential.i = 25
(SelfReferential.sr)->i = 25
(SelfReferential.sr)->sr->i = 25
(SelfReferential.sr)->sr->sr->i = 25
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
// Allowing a struct to refer to itself
typedef struct SelfReferential // type names `SelfReferential',
{ // `struct SelfReferential' defined in C++
int i;
struct SelfReferential* sr; // SelfReferential* sr;
} SelfReferential; // superfluous in C++
// (works without typedef...SelfReferential):
/*
struct SelfReferential // type names `SelfReferential',
{ // `struct SelfReferential' defined in C++
int i;
struct SelfReferential* sr; // SelfReferential* sr;
};
*/
int main()
{
struct SelfReferential sr1, sr2; // SelfReferential sr1, sr2;
sr1.sr = &sr2;
sr2.sr = &sr1;
sr1.i = 47;
sr2.i = 1024;
cout << "sr1.i = " << sr1.i << endl; // 47
cout << "(sr2.sr)->i = " << (sr2.sr)->i << endl; // 47
cout << "sr2.i = " << sr2.i << endl; // 1024
cout << "(sr2.sr)->sr->i = " << (sr2.sr)->sr->i << endl; // 1024
cout << "(sr1.sr)->sr->sr->i = " << (sr1.sr)->sr->sr->i << endl; // 1024
SelfReferential SelfReferential; // `SelfReferential' becomes a variable name
// SelfReferential sr3; // `SelfReferential' is no longer a type name
struct SelfReferential sr3; // `struct SelfReferential' is still a type name
SelfReferential.sr = &SelfReferential; // truly SelfReferential
SelfReferential.i = 25;
cout << "SelfReferential.i = " << SelfReferential.i << endl; // 25
cout << "(SelfReferential.sr)->i = " << (SelfReferential.sr)->i
<< endl; // 25
cout << "(SelfReferential.sr)->sr->i = " << (SelfReferential.sr)->sr->i
<< endl; // 25
cout << "(SelfReferential.sr)->sr->sr->i = "
<< (SelfReferential.sr)->sr->sr->i << endl; // 25
return 0;
}
/*
g++ SelfReferential.cpp -o SelfReferential
./SelfReferential
sr1.i = 47
(sr2.sr)->i = 47
sr2.i = 1024
(sr2.sr)->sr->i = 1024
(sr1.sr)->sr->sr->i = 1024
SelfReferential.i = 25
(SelfReferential.sr)->i = 25
(SelfReferential.sr)->sr->i = 25
(SelfReferential.sr)->sr->sr->i = 25
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
// Using pointers to structs
typedef struct Structure3 // type name `struct Structure3' defined in C
{
char c;
int i;
float f;
double d;
} Structure3; // type name `Structure3' defined here
void printStructure3(struct Structure3 s);
void printStructure3(struct Structure3);
void printStructure3(Structure3 s);
void printStructure3(Structure3);
int main()
{
struct Structure3 s1, s2; // Structure3 s1, s2;
struct Structure3* sp = &s1; // Structure3* sp = &s1;
(*sp).c = 'a'; // sp->c
sp->i = 1; // (*sp).i
sp->f = 3.14;
sp->d = 0.00093;
printStructure3(s1);
printStructure3(*sp); // s1
Structure3 Structure3; // `Structure3' no longer type name
sp = &s2; // Point to different struct object (`sp' still works)
sp->c = 'b';
sp->i = 2;
sp->f = 6.28;
sp->d = 0.00186;
printStructure3(s1); // `s1' retains its values
printStructure3(s2);
printStructure3(*sp); // s2
// Structure3* ssp = sp; // compile error: `Structure3' no longer type name
struct Structure3* ssp = sp;
printStructure3(*ssp);
return 0;
}
void printStructure3(Structure3 s)
{
printf("c = %c (%d), i = %d, f = %g, d = %g\n", s.c, s.c, s.i, s.f, s.d);
}
/*
gcc simplestruct3.c -o simplestruct3
./simplestruct3
c = a (97), i = 1, f = 3.14, d = 0.00093
c = a (97), i = 1, f = 3.14, d = 0.00093
c = a (97), i = 1, f = 3.14, d = 0.00093
c = b (98), i = 2, f = 6.28, d = 0.00186
c = b (98), i = 2, f = 6.28, d = 0.00186
c = b (98), i = 2, f = 6.28, d = 0.00186
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
// Using pointers to structs
typedef struct Structure3 // type names `Structure3',
{ // `struct Structure3' defined in C++
char c;
int i;
float f;
double d;
} Structure3; // superfluous in C++
// (works without typedef...Structure3):
/*
struct Structure3 // type names `Structure3',
{ // `struct Structure3' defined in C++
char c;
int i;
float f;
double d;
};
*/
void printStructure3(struct Structure3 s);
void printStructure3(struct Structure3);
void printStructure3(Structure3 s);
void printStructure3(Structure3);
int main()
{
struct Structure3 s1, s2; // Structure3 s1, s2;
struct Structure3* sp = &s1; // Structure3* sp = &s1;
(*sp).c = 'a'; // sp->c
sp->i = 1; // (*sp).i
sp->f = 3.14;
sp->d = 0.00093;
printStructure3(s1);
printStructure3(*sp); // s1
Structure3 Structure3; // `Structure3' no longer type name
sp = &s2; // Point to different struct object (`sp' still works)
sp->c = 'b';
sp->i = 2;
sp->f = 6.28;
sp->d = 0.00186;
printStructure3(s1); // `s1' retains its values
printStructure3(s2);
printStructure3(*sp); // s2
// Structure3* ssp = sp; // compile error: `Structure3' no longer type name
struct Structure3* ssp = sp;
printStructure3(*ssp);
return 0;
}
void printStructure3(Structure3 s)
{
cout << "c = " << s.c << " (" << int(s.c) << "), i = " << s.i
<< ", f = " << s.f << ", d = " << s.d << endl;
}
/*
g++ SimpleStruct3.cpp -o SimpleStruct3
./SimpleStruct3
c = a (97), i = 1, f = 3.14, d = 0.00093
c = a (97), i = 1, f = 3.14, d = 0.00093
c = a (97), i = 1, f = 3.14, d = 0.00093
c = b (98), i = 2, f = 6.28, d = 0.00186
c = b (98), i = 2, f = 6.28, d = 0.00186
c = b (98), i = 2, f = 6.28, d = 0.00186
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 3-15. Create a struct that holds two string objects and one int. Use a typedef for the struct name. Create an instance of the struct, initialize all three values in your instance, and print them out. Take the address of your instance and assign it to a pointer to your struct type. Change the three values in your instance and print them out, all using the pointer.
#include <stdio.h> // for printf()
#include <string.h> // for strcpy()
#define SIZE 100
typedef struct Student // type name `struct Student' defined in C
{
int age;
char firstName[SIZE];
char lastName[SIZE];
} Student; // type name `Student' defined here
void print(struct Student s);
void print(struct Student);
void print(Student s);
void print(Student);
int main()
{
struct Student s; // Student s;
s.age = 18;
strcpy(s.firstName, "John");
strcpy(s.lastName, "Doe");
print(s);
struct Student* sp = &s; // Student* sp = &s;
(*sp).age = 19; // sp->age
strcpy(sp->firstName, "Jane"); // (*sp).firstName
strcpy(sp->lastName, "Dove");
print(*sp); // s
return 0;
}
void print(Student s)
{
printf("%s %s (%d)\n", s.firstName, s.lastName, s.age);
}
/*
gcc student.c -o student
./student
John Doe (18)
Jane Dove (19)
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
typedef struct Student // type names `Student',
{ // `struct Student' defined in C++
int age;
string firstName;
string lastName;
} Student; // superfluous in C++
// (works without typedef...Student):
/*
struct Student // type names `Student',
{ // `struct Student' defined in C++
int age;
string firstName;
string lastName;
};
*/
void print(struct Student s);
void print(struct Student);
void print(Student s);
void print(Student);
int main()
{
struct Student s; // Student s;
s.age = 18;
s.firstName = "John";
s.lastName = "Doe";
print(s);
struct Student* sp = &s; // Student* sp = &s;
(*sp).age = 19; // sp->age
sp->firstName = "Jane"; // (*sp).firstName
sp->lastName = "Dove";
print(*sp); // s
return 0;
}
void print(Student s)
{
cout << s.firstName << " " << s.lastName
<< " (" << s.age << ")" << endl;
}
/*
g++ Student.cpp -o Student
./Student
John Doe (18)
Jane Dove (19)
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
// Keeping track of shapes
enum ShapeType // type `enum ShapeType' defined in C
{
circle, square, rectangle // 0, 1, 2
}; // Must end with a semicolon like a struct
void printShape(enum ShapeType s);
void printShape(enum ShapeType);
// void printShape(ShapeType); // warning: variable `ShapeType' has no type
// void printShape(ShapeType s); // error: unknown type name `ShapeType'
typedef enum ShapeType ShapeType; // `ShapeType' becomes a type name
void printShape(ShapeType s);
void printShape(ShapeType);
int main()
{
enum ShapeType shape = circle; // ShapeType shape = circle;
// Activities here....
// circle++; // compile error: `circle' not lvalue
// circle = rectangle; // compile error: `circle' not lvalue
printf("circle: %d, shape: %d\n", circle, shape);
shape++; // OK in C, error in C++
shape = 1; // OK in C, error in C++
shape = square; // OK in C/C++
printf("square: %d, shape: %d\n", square, shape);
shape += 2; // OK in C, error in C++
shape = rectangle+1; // OK in C, error in C++
shape = rectangle; // OK in C/C++
printf("rectangle: %d, shape: %d\n", rectangle, shape);
shape--, shape++; // OK in C, error in C++
printf("shape - circle = %d\n", shape-circle);
printf("circle - rectangle = %d\n", circle-rectangle);
printf("shape + square = %d\n", shape+square);
// Now do something based on what the shape is:
printShape(0); // circle, printShape() accepts both ShapeType, int as arg
printShape(square); // square, argument ShapeType
printShape(shape); // rectangle, argument ShapeType
printShape(shape+1); // unknown shape, argument int
return 0;
}
void printShape(ShapeType shape)
{
switch(shape)
{
case circle:
printf("circle\n");
/* circle stuff */
break;
case square:
printf("square\n");
/* square stuff */
break;
case rectangle:
printf("rectangle\n");
/* rectangle stuff */
break;
default:
printf("unknown shape\n");
break;
}
}
/*
gcc enum.c -o enum
./enum
circle: 0, shape: 0
square: 1, shape: 1
rectangle: 2, shape: 2
shape - circle = 2
circle - rectangle = -2
shape + square = 3
circle
square
rectangle
unknown shape
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
// Keeping track of shapes
enum ShapeType // types `ShapeType', `enum ShapeType' defined in C++
{
circle, square, rectangle // 0, 1, 2
}; // Must end with a semicolon like a struct
void printShape(enum ShapeType s);
void printShape(enum ShapeType);
void printShape(ShapeType s);
void printShape(ShapeType);
void printShape(int); // overloading printShape()
int main()
{
enum ShapeType shape = circle; // ShapeType shape = circle;
// Activities here....
// circle++; // compile error
// circle = rectangle; // compile error: `circle' not lvalue
cout << "circle: " << circle << ", shape: " << shape << endl;
// shape++; // compile error
// shape = 1; // compile error
shape = square;
cout << "square: " << square << ", shape: " << shape << endl;
// shape += 2; // compile error
// shape = rectangle+1; // compile error
shape = rectangle;
cout << "rectangle: " << rectangle << ", shape: " << shape << endl;
// shape--; // compile error
cout << "shape - circle = " << shape-circle << endl;
cout << "circle - rectangle = " << circle-rectangle << endl;
cout << "shape + square = " << shape+square << endl;
// Now do something based on what the shape is:
printShape(0); // overloaded function, prints `0: circle'
printShape(square); // square
printShape(shape); // rectangle
printShape(shape+1); // overloaded function, prints `3: unknown shape'
return 0;
}
void printShape(ShapeType shape)
{
switch(shape)
{
case circle:
cout << "circle" << endl;
/* circle stuff */
break;
case square:
cout << "square" << endl;
/* square stuff */
break;
case rectangle:
cout << "rectangle" << endl;
/* rectangle stuff */
break;
default:
cout << "unknown shape" << endl;
break;
}
}
void printShape(int i)
{
switch(i)
{
case circle:
cout << i << ": circle" << endl;
/* circle stuff */
break;
case square:
cout << i << ": square" << endl;
/* square stuff */
break;
case rectangle:
cout << i << ": rectangle" << endl;
/* rectangle stuff */
break;
default:
cout << i << ": unknown shape" << endl;
break;
}
}
/*
g++ Enum.cpp -o Enum
./Enum
circle: 0, shape: 0
square: 1, shape: 1
rectangle: 2, shape: 2
shape - circle = 2
circle - rectangle = -2
shape + square = 3
0: circle
square
rectangle
3: unknown shape
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 3-16. Create a program that uses an enumeration of colors. Create a variable of this enum type and print out all the numbers that correspond with the color names, using a for() loop.
#include <stdio.h> // for printf(), putchar()
typedef enum Colors // type `enum Colors' defined in C
{
white, yellow, green, blue, orange, red, brown, black // 0, 1, ..., 7
} Colors; // type `Colors' defined in C
void printColor(enum Colors);
void printColor(Colors);
int main()
{
enum Colors color; // Colors color;
for (color = white; color <= black; color++)
{printColor(color);}
printColor(color);
putchar('\n');
int col;
for (col = white; col <= black; col++)
{printColor(col);}
printColor(col);
return 0;
}
void printColor(Colors color)
{
switch(color)
{
case white:
printf("white: %d\n", color);
break;
case yellow:
printf("yellow: %d\n", color);
break;
case green:
printf("green: %d\n", color);
break;
case blue:
printf("blue: %d\n", color);
break;
case orange:
printf("orange: %d\n", color);
break;
case red:
printf("red: %d\n", color);
break;
case brown:
printf("brown: %d\n", color);
break;
case black:
printf("black: %d\n", color);
break;
default:
printf("unknown color: %d\n", color);
break;
}
}
/*
gcc colors.c -o colors
./colors
white: 0
yellow: 1
green: 2
blue: 3
orange: 4
red: 5
brown: 6
black: 7
unknown color: 8
white: 0
yellow: 1
green: 2
blue: 3
orange: 4
red: 5
brown: 6
black: 7
unknown color: 8
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
enum Colors // types `Colors', `enum Colors' defined in C++
{
white, yellow, green, blue, orange, red, brown, black // 0, 1, ..., 7
};
void printColor(enum Colors);
void printColor(Colors);
int main()
{
enum Colors color; // Colors color;
for (color = white; color <= black; color = Colors(color+1))
{printColor(color);}
printColor(color);
cout << endl;
int col;
for (col = white; col <= black; col++)
{printColor(Colors(col));} // int to Colors
printColor(Colors(col));
return 0;
}
void printColor(Colors color)
{
switch(color)
{
case white:
cout << "white: " << color << endl;
break;
case yellow:
cout << "yellow: " << color << endl;
break;
case green:
cout << "green: " << color << endl;
break;
case blue:
cout << "blue: " << color << endl;
break;
case orange:
cout << "orange: " << color << endl;
break;
case red:
cout << "red: " << color << endl;
break;
case brown:
cout << "brown: " << color << endl;
break;
case black:
cout << "black: " << color << endl;
break;
default:
cout << "unknown color: " << color << endl;
break;
}
}
/*
g++ Colors.cpp -o Colors
./Colors
white: 0
yellow: 1
green: 2
blue: 3
orange: 4
red: 5
brown: 6
black: 7
unknown color: 8
white: 0
yellow: 1
green: 2
blue: 3
orange: 4
red: 5
brown: 6
black: 7
unknown color: 8
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// The size and simple use of a union in C
#include <stdio.h> // for printf()
// Declaration similar to a struct or class:
typedef union Packed // type `union Packed' defined in C
{
char i;
short j;
int k;
long l;
float f;
double d; // The union will be the size of a double,
// since that's the largest element
} Packed; // type `Packed' defined in C
// Semicolon ends a union definition, like a struct definition
void printUnion(union Packed);
void printUnion(Packed);
int main()
{
printf("sizeof(Packed) = %lu\n", sizeof(union Packed)); // sizeof(Packed)
printf("sizeof(double) = %lu\n", sizeof(double));
union Packed x; // Packed x;
printUnion(x);
x.i = 'c'; // ASCII 99
printUnion(x);
x.d = 3.14159;
printUnion(x);
return 0;
}
void printUnion(Packed p)
{
printf("i = %c (%d), j = %d, k = %d, l = %ld, f = %g, d = %g\n",
p.i, p.i, p.j, p.k, p.l, p.f, p.d);
}
/*
gcc union.c -o union
./union
sizeof(Packed) = 8
sizeof(double) = 8
i = (0), j = 0, k = 0, l = 0, f = 0, d = 0
i = c (99), j = 99, k = 99, l = 99, f = 1.38729e-43, d = 4.89125e-322
i = n (110), j = -31122, k = -266631570, l = 4614256650576692846, f = -1.92531e+29, d = 3.14159
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// The size and simple use of a union in C++
#include <iostream>
using std::cout;
using std::endl;
// Declaration similar to a struct or class:
union Packed // types `union Packed', `Packed' defined in C++
{
char i;
short j;
int k;
long l;
float f;
double d; // The union will be the size of a double,
// since that's the largest element
}; // Semicolon ends a union, like a struct
void printUnion(union Packed);
void printUnion(Packed);
int main()
{
cout << "sizeof(Packed) = " << sizeof(union Packed) << endl; // sizeof(Packed)
printf("sizeof(double) = %lu\n", sizeof(double));
union Packed x; // Packed x;
printUnion(x);
x.i = 'c';
printUnion(x);
x.d = 3.14159;
printUnion(x);
return 0;
}
void printUnion(Packed p)
{
cout << "i = " << p.i << ", j = " << p.j << ", k = " << p.k
<< ", l = " << p.l << ", f = " << p.f << ", d = " << p.d << endl;
}
/*
g++ Union.cpp -o Union
./Union
sizeof(Packed) = 8
sizeof(double) = 8
i = p, j = 4464, k = -813231760, l = 140728085188976, f = -4.53213e+09, d = 6.95289e-310
i = c, j = 4451, k = -813231773, l = 140728085188963, f = -4.53213e+09, d = 6.95289e-310
i = n, j = -31122, k = -266631570, l = 4614256650576692846, f = -1.92531e+29, d = 3.14159
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), putchar()
typedef union Union1 // type `union Union1' defined in C
{
char c1;
char c2;
} Union1; // type `Union1' defined in C
typedef union Union2
{
char c;
short sh;
} Union2;
typedef union Union3
{
int i;
long l;
long long ll;
} Union3;
typedef union Union4
{
float f;
double d;
long double ld;
} Union4;
int main()
{
printf("sizeof(char) = %lu\n", sizeof(char));
printf("sizeof(Union1) = %lu\n", sizeof(union Union1)); // sizeof(Union1)
union Union1 u1; // Union1 u1;
printf("c1 = %c (%d), c2 = %c (%d)\n", u1.c1, u1.c1, u1.c2, u1.c2);
u1.c1 = 'a'; // ASCII value 97
printf("c1 = %c (%d), c2 = %c (%d)\n", u1.c1, u1.c1, u1.c2, u1.c2);
u1.c2 = 'b'; // ASCII value 98
printf("c1 = %c (%d), c2 = %c (%d)\n", u1.c1, u1.c1, u1.c2, u1.c2);
putchar('\n');
printf("sizeof(char) = %lu\n", sizeof(char));
printf("sizeof(short) = %lu\n", sizeof(short));
printf("sizeof(Union2) = %lu\n", sizeof(Union2));
Union2 u2;
printf("c = %c (%d), sh = %d\n", u2.c, u2.c, u2.sh);
u2.c = 'c'; // ASCII value 99
printf("c = %c (%d), sh = %d\n", u2.c, u2.c, u2.sh);
u2.sh = 100; // ASCII value for 'd'
printf("c = %c (%d), sh = %d\n", u2.c, u2.c, u2.sh);
putchar('\n');
printf("sizeof(int) = %lu\n", sizeof(int));
printf("sizeof(long) = %lu\n", sizeof(long));
printf("sizeof(long long) = %lu\n", sizeof(long long));
printf("sizeof(Union3) = %lu\n", sizeof(Union3));
Union3 u3;
printf("i = %d, l = %ld, ll = %lld\n", u3.i, u3.l, u3.ll); // decimal
printf("i = %x, l = %lx, ll = %llx\n", u3.i, u3.l, u3.ll); // hex
u3.i = 1;
printf("i = %d, l = %ld, ll = %Ld\n", u3.i, u3.l, u3.ll); // decimal
u3.l = 1000000;
printf("i = %d, l = %ld, ll = %Ld\n", u3.i, u3.l, u3.ll);
u3.ll = 1000000000;
printf("i = %d, l = %ld, ll = %Ld\n", u3.i, u3.l, u3.ll);
putchar('\n');
printf("sizeof(float) = %lu\n", sizeof(float));
printf("sizeof(double) = %lu\n", sizeof(double));
printf("sizeof(long double) = %lu\n", sizeof(long double));
printf("sizeof(Union4) = %lu\n", sizeof(Union4));
Union4 u4;
printf("f = %g, d = %g, ld = %Lg\n", u4.f, u4.d, u4.ld);
u4.f = 1000000000;
printf("f = %g, d = %g, ld = %Lg\n", u4.f, u4.d, u4.ld);
u4.d = 1000000000000;
printf("f = %g, d = %g, ld = %Lg\n", u4.f, u4.d, u4.ld);
u4.ld = 1000000000000000;
printf("f = %g, d = %g, ld = %Lg\n", u4.f, u4.d, u4.ld);
return 0;
}
/*
gcc unions.c -o unions
./unions
sizeof(char) = 1
sizeof(Union1) = 1
c1 = (0), c2 = (0)
c1 = a (97), c2 = a (97)
c1 = b (98), c2 = b (98)
sizeof(char) = 1
sizeof(short) = 2
sizeof(Union2) = 2
c = (0), sh = 0
c = c (99), sh = 99
c = d (100), sh = 100
sizeof(int) = 4
sizeof(long) = 8
sizeof(long long) = 8
sizeof(Union3) = 8
i = -1594085248, l = 94530636099712, ll = 94530636099712 // decimal
i = a0fc3080, l = 55f9a0fc3080, ll = 55f9a0fc3080 // hexadecimal
i = 1, l = 94527935217665, ll = 94527935217665 // decimal
i = 1000000, l = 1000000, ll = 1000000
i = 1000000000, l = 1000000000, ll = 1000000000
sizeof(float) = 4
sizeof(double) = 8
sizeof(long double) = 16
sizeof(Union4) = 16
f = 7.70068e-34, d = 6.95251e-310, ld = 5.12954e-4937
f = 1e+09, d = 6.95257e-310, ld = 5.12958e-4937
f = -1.73472e-18, d = 1e+12, ld = 1.74478e-4932
f = -1.0842e-19, d = -4.77948e+170, ld = 1e+15
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
using std::dec; // decimal
using std::hex; // hexadecimal
union Union1 // types `union Union1', `Union1' defined in C++
{
char c1;
char c2;
};
union Union2
{
char c;
short sh;
};
union Union3
{
int i;
long l;
long long ll;
};
union Union4
{
float f;
double d;
long double ld;
};
int main()
{
cout << "sizeof(char) = " << sizeof(char) << endl;
cout << "sizeof(Union1) = " << sizeof(union Union1) << endl;
union Union1 u1; // Union1 u1;
cout << "c1 = " << u1.c1 << "(" << int(u1.c1) << "), c2 = " << u1.c2
<< "(" << int(u1.c2) << ")" << endl;
u1.c1 = 'a'; // ASCII value 97
cout << "c1 = " << u1.c1 << "(" << int(u1.c1) << "), c2 = " << u1.c2
<< "(" << int(u1.c2) << ")" << endl;
u1.c2 = 'b'; // ASCII value 98
cout << "c1 = " << u1.c1 << "(" << int(u1.c1) << "), c2 = " << u1.c2
<< "(" << int(u1.c2) << ")" << endl;
cout << endl;
cout << "sizeof(char) = " << sizeof(char) << endl;
cout << "sizeof(short) = " << sizeof(short) << endl;
cout << "sizeof(Union2) = " << sizeof(Union2) << endl;
Union2 u2;
cout << "c = " << u2.c << "(" << int(u2.c) << "), sh = " << u2.sh << endl;
u2.c = 'c'; // ASCII value 99
cout << "c = " << u2.c << "(" << int(u2.c) << "), sh = " << u2.sh << endl;
u2.sh = 100; // ASCII value for 'd'
cout << "c = " << u2.c << "(" << int(u2.c) << "), sh = " << u2.sh << endl;
cout << endl;
cout << "sizeof(int) = " << sizeof(int) << endl;
cout << "sizeof(long) = " << sizeof(long) << endl;
cout << "sizeof(long long) = " << sizeof(long long) << endl;
cout << "sizeof(Union3) = " << sizeof(Union3) << endl;
Union3 u3;
cout << "i = " << u3.i << ", l = " << u3.l << ", ll = " << u3.ll << endl; // decimal
cout << "i = " << hex << u3.i << ", l = " << u3.l // hex
<< ", ll = " << u3.ll << dec << endl; // at the end, revert to decimal
u3.i = 1;
cout << "i = " << u3.i << ", l = " << u3.l << ", ll = " << u3.ll << endl;
u3.l = 1000000; // decimal
cout << "i = " << u3.i << ", l = " << u3.l << ", ll = " << u3.ll << endl;
u3.ll = 1000000000;
cout << "i = " << u3.i << ", l = " << u3.l << ", ll = " << u3.ll << endl;
cout << endl;
cout << "sizeof(float) = " << sizeof(float) << endl;
cout << "sizeof(double) = " << sizeof(double) << endl;
cout << "sizeof(long double) = " << sizeof(long double) << endl;
cout << "sizeof(Union4) = " << sizeof(Union4) << endl;
Union4 u4;
cout << "f = " << u4.f << ", d = " << u4.d << ", ld = " << u4.ld << endl;
u4.f = 1000000000;
cout << "f = " << u4.f << ", d = " << u4.d << ", ld = " << u4.ld << endl;
u4.d = 1000000000000;
cout << "f = " << u4.f << ", d = " << u4.d << ", ld = " << u4.ld << endl;
u4.ld = 1000000000000000;
cout << "f = " << u4.f << ", d = " << u4.d << ", ld = " << u4.ld << endl;
return 0;
}
/*
g++ Unions.cpp -o Unions
./Unions
sizeof(char) = 1
sizeof(Union1) = 1
c1 = (0), c2 = (0)
c1 = a(97), c2 = a(97)
c1 = b(98), c2 = b(98)
sizeof(char) = 1
sizeof(short) = 2
sizeof(Union2) = 2
c = (0), sh = 0
c = c(99), sh = 99
c = d(100), sh = 100
sizeof(int) = 4
sizeof(long) = 8
sizeof(long long) = 8
sizeof(Union3) = 8
i = 328057344, l = 94596982751744, ll = 94596982751744 // decimal
i = 138dc200, l = 5609138dc200, ll = 5609138dc200 // hexadecimal
i = 1, l = 94596654694401, ll = 94596654694401 // decimal
i = 1000000, l = 1000000, ll = 1000000
i = 1000000000, l = 1000000000, ll = 1000000000
sizeof(float) = 4
sizeof(double) = 8
sizeof(long double) = 16
sizeof(Union4) = 16
f = -3.00175e+16, d = 6.95269e-310, ld = 5.12967e-4937
f = 1e+09, d = 6.95257e-310, ld = 5.12958e-4937
f = -1.73472e-18, d = 1e+12, ld = 1.74478e-4932
f = -1.0842e-19, d = -4.77948e+170, ld = 1e+15
*/