ch3-SimpleStruct1 (struct usage in C/C++)

Chapter_3     Exercise_3-12     typedef SimpleStruct2     Exercise_3-13







SimpleStruct1     TCP1, p. 189-190  (simplestruct1.c,  SimpleStruct1.cpp)




Note:  See also ch2-Declare in Chapter_2, Section ch2-Streams.




simplestruct1.c         download


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











SimpleStruct1.cpp         download


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









Chapter_3     Exercise_3-12     typedef BACK_TO_TOP SimpleStruct2     Exercise_3-13



Comments

Popular posts from this blog

Contents