// -- C ++ short cut tutorial -- Chapter 6 -- pointer (Part 2)
// -- Reading Notes -- Chapter 6 -- pointer
// -- 11/13/2005 sun.
// -- Computer lab
// -- Liwei
// -- Program #8 pointer and string literal volume
# Include <iostream>
Using namespace STD;
Int main ()
{
Char * s;
S = "pointer are fun to use./N ";
Cout <s;
Return 0;
}
// -- Program #9 example of a pointer comparison operation
# Include <iostream>
Using namespace STD;
Int main ()
{
Int num [10];
Int * Start, * end;
// Start = num ;//
Start = & num [0];
// End = & num [9]; //
End = start + 9;
While (start <= end)
{
Cout <"enter a number :";
Cin> * Start ++;
}
Start = num;
While (start <= end)
{
Cout <* Start ++ <'';
}
Cout <Endl;
Return 0;
}
// -- Program #10 example of a pointer Array
# Include <iostream>
# Include <cstdlib>
# Include <conio. h>
Using namespace STD;
Char * fortunes [] = {
"Soon, you will come in to some money./N ",
"A new love will enter your life./N ",
"You will live long and prosper./N ",
"Now is a good time to invest for the future./N ",
"A close friend will ask for a favor./N"
};
Int main ()
{
Int chance;
Cout <"to see your fortune, press a key :";
While (! Kbhit () chance = rand ();
Cout <Endl;
// Chance = rand ();
Chance = chance & 5;
Cout <fortunes [Chance];
Cout <Endl;
Getchar ();
Return 0;
}
// -- Program #11 example of a pointer Array
# Include <iostream>
# Include <cstdlib>
# Include <conio. h>
Using namespace STD;
Char * keyword [] [2] = {
"For", "For ",
"If", "If if ",
"Switch", "Switch switch ",
"While", "while ",
"/0 ",""
};
Int main ()
{
Char STR [80];
Int I;
Cout <"enter keyword :";
Cin> STR;
For (I = 0; * keyword [I] [1]; I ++)
If (! Strcmp (keyword [I] [0], STR ))
Cout <keyword [I] [1];
Cout <Endl <"=" <Endl;
Return 0;
}
// -- Program #12 describes multiple indirect usage
# Include <iostream>
Using namespace STD;
Int main ()
{
Int X, * P, ** Q;
X = 10;
P = & X;
Q = & P;
Cout <x <''<* P <'' <** q <Endl;
Cout <Endl <"=" <Endl;
Return 0;
}
// -- Program #13 the program is incorrect.
# Include <iostream>
Using namespace STD;
Int main ()
{
Int X, * P;
X = 10;
* P = x; // P is not initialized.
Cout <x <''<* P;
Cout <Endl <"=" <Endl;
Return 0;
}
// -- Program #14 programs that forget to reset the pointer
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace STD;
Int main ()
{
Char s [80];
Char * pl;
// PL = s;
Do {
PL = s;
Cout <"enter a string :";
Gets (PL );
While (* PL)
Cout <(INT) * PL ++ <'';
Cout <Endl;
} While (strcmp (S, "done "));
Cout <Endl <"=" <Endl;
Return 0;
}