[ fromfile: classes.xml id: structs ]
In the C language, there is the struct keyword, for defining a structured chunk of memory.
Example 2.1. src/structdemo/demostruct.h
[ . . . . ]
struct Fraction {
int numer, denom;
string description;
};
[ . . . . ]
Example 2.1 shows the definition of a structured piece of memory, comprised of smaller chunks of memory.
Each smaller chunk (numer, denom, description) is accessible by name.
Example 2.2. src/structdemo/demostruct.cpp
[ . . . . ]
void printFraction(Fraction f) {
cout << f.numer << "/" << f.denom << endl;
cout << " =? " << f.description << endl;
}
int main() {
Fraction f1;
f1.numer = 4;
f1.denom = 5;
f1.description = "four fifths";
Fraction f2 = {2, 3, "two thirds"};
f1.numer = f1.numer + 2;
printFraction(f1);
printFraction(f2);
return 0;
}
Output:
6/5
=? four fifths
2/3
=? two thirds
Example 2.2 shows how we can use a structured chunk of memory (containing subobjects) as a single entity.
| Generated: $Date: 2009-09-08 12:15:32 -0400 (Tue, 08 Sep 2009) $ | © 2009 Alan Ezust and Paul Ezust. |