ch3-SimpleStruct2 (typedef struct in C/C++)

Chapter_3     Exercise_3-12     SimpleStruct1 SelfReferential     Exercise_3-13







SimpleStruct2     TCP1, p. 190  (simplestruct2.c,  SimpleStruct2.cpp)




simplestruct2.c         download


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











SimpleStruct2.cpp         download


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









Chapter_3     Exercise_3-12     SimpleStruct1 BACK_TO_TOP SelfReferential     Exercise_3-13



Comments

Popular posts from this blog

Contents