ch3-SimpleStruct3 (typedef, struct pointers in C/C++)

Chapter_3     Exercise_3-12     SelfReferential Exercise_3-15     Exercise_3-13







SimpleStruct3     TCP1, p. 191-192  (simplestruct3.c,  SimpleStruct3.cpp)




simplestruct3.c         download


#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;

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











SimpleStruct3.cpp         download


#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;

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









Chapter_3     Exercise_3-12     SelfReferential BACK_TO_TOP Exercise_3-15     Exercise_3-13



Comments

Popular posts from this blog

Contents