struct foo {
int foo_id;
char foo_name[32];
float foo_weight;
};
This is the declaration of the structure. It tells the compiler what
the parts are and thus how big the structure is. In this case, the size
of the structure is the sum of the sizes of an int, a 32 element array
of characters and a float. Once you have declared a struct, you can use
it anywhere (mostly) you can use other data types.
For example,
struct foo a; // a single structure called a
struct foo arr[10]; // an array of 10 structures
struct foo * foo_ptr; // a pointer to a structure
The way you get at the pieces in a struct depend on whether you have
an actual variable or a pointer.
So, if we have
struct foo b, *foo_ptr;
foo_ptr = &b; // & gets the address of the variable
We can set the value of foo_id by either
b.foo_id=8;
foo_ptr->foo_id = 8;
Arrays work the same way as with other data types.
struct foo foo_arr[10];
foo_arr[0].foo_id = 4;
foo_ptr = foo_arr; // notice no ampersand
foo_ptr-> foo_id = 4;
To change the next one, we can either do arithmetic on the foo_ptr
foo_ptr++;
foo_ptr->foo_id=6;
or
(foo_ptr+1)->foo_id=6;
The second way doesn't actually change the pointer. Pointers to arrays
of structures work the same way as they do when working with arrays of
characters and anything else.
They type of foo_ptr->foo_id is integer so it can be used anywhere
an integer can be used. Specifically,
cout << foo_ptr -> foo_id << endl;
prints the foo_id as an integer.
There are several kinds of control structures in C++. There are the IF, WHILE, DO and FOR loops. There is also the conditional expression.
The expression part calls user_count() and copies the value to a. Since the value of the assignment is the right hand side, we then check the value returned by user_count() with 1 and if it is 1, we print the message.
There are a number of comparison operators in C++
Operator | Meaning |
= | assignment |
== | comparison |
< | less than |
> | greater than |
<= | less than or equal to |
>= | greater than or equal to |
!= | not equal |
These all assume that the values being compared are integers or floats
or can be treated as a number. There are also a number of logical operators.
See page 58.
Operator | Meaning |
&& | and |
|| (2 vertical bar characters) | or |
! (exclamation point) | not |
These can be combined in many ways.
int a, b,c;
if ( (a < 6) && (b> 7) ) cout << "case 1 " <<
endl;
if ( (a < 6) || (b> 7) ) cout << "case 2 " << endl;
if ( ( (a < 6) && (b> 7 ) || ( (c
-b < a) && !c ) ) cout << "case 3 " << endl;
It is important to remember that in these cases, we use the fact that zero is false and anything else is true. Typically the value of a comparison operator is either 0 or 1.
Also be careful, there is a big difference between && and &.
The single ampersand means do a bit wise logical and. See page 63.
The statement part can be one or more actual statements. So, if there is one statement, it looks like
while (a < b) cout << a << endl;
If there are several statements
while( a<b) {
cout << a << endl;
a += 3;
} // end while
The evaluation of the expression is doen at the top of the loop, so
if the expression is false, it won't run the loop even once.
The statement is done at least once since we don't check until the bottom of the loop.
The first expression is the initialization. Examples include i=0, int
i=6.
The second expression is the test expression. If it is true, then we
do this time through the loop.
The third expression is the increment. It is commonly i++.
For example,
for(int i=0; i < 10; i++) cout << i << endl;
This starts by creating an i variable and setting it to 0. Since 0
< 10, we run the body of the loop and 0 is printed. Then we do the increment
and i becomes 1. We check again and 1 is < 10 so we print 1. and so
on. After we print 9, i is incremented to 10. 10 is not less then 10 so
we don't do the body of the loop.
So this prints 0-9.
This loop is equivalent to the while loop
int i = 0;
while (i < 10) {
cout << i << endl;
i++;
} // endwhile
You can leave off parts of the for loop. For example, if we already
have a variable counter used for something and now we want
to use it a loop variable, we can skip the initialization.
for( ; counter < 20; counter++)
We could also leave off the increment is we were doing different increments
for (; counter < 20;) {
if (counter == 15)
counter *= 2;
else
counter++;
} // end
while(true) {
if( X < 25) break;
} // endwhile
This will stop if X >= 25.
This works on the other loops as well.
The continue will go back to the top of the loop. in a while or do, this cause the expression to be evaluated. In a for loop it causes the increment to be done.