// -- C ++ shortcut tutorial -- Chapter 6 -- pointer (Part 1)
// -- Reading Notes -- Chapter 6 -- pointer
// -- 11/12/2005 sat.
// -- Computer lab
// -- Liwei
// -- Program #1
# Include <iostream>
Using namespace STD;
Int main ()
{
Int balance;
Int * balptr;
Int value;
Balance = 3200;
Balptr = & balance;
Cout <balptr <Endl;
Value = * balptr ++; // note this usage
Cout <"balance is:" <value <'/N' <balptr <''<* balptr <Endl;
Return 0;
}
// -- Program #2
# Include <iostream>
Using namespace STD;
Int main ()
{
Double X, Y;
Int * P;
X = 123.23;
P = (int *) & X; // convert the pointer of the double type to the int type
Y = * P;
Cout <Y <''<* P <Endl;
Return 0;
}
// -- Program #3 assign values by pointer
# Include <iostream>
Using namespace STD;
Int main ()
{
Int * P, num;
P = & num;
* P = 100;
Cout <num <'';
(* P) ++;
Cout <num <'';
(* P )--;
Cout <num <'/N ';
Return 0;
}
// -- Program #4: pointer operation
# Include <iostream>
Using namespace STD;
Int main ()
{
Char * c, d [10];
Wchar_t * CC, Dd [10];
Int * I, j [10];
Float * m, n [10];
Double * f, g [10];
Int X;
C = D;
Cc = dd;
I = J;
M = N;
F = g;
Cout <"= char =" <"wchar_t =" <"Int =" <"float =" <"double === "<'/N ';
For (x = 0; x <10; X ++)
Cout <(INT) c + x <''<CC + x <'' <I + x <''<m + x <'' <F + x <<'/N '; // remember (INT) C but cannot (INT) CC
Return 0;
}
// -- Program #5. Mark extraction program: pointer version
# Include <iostream>
Using namespace STD;
Int main ()
{
Char STR [40];
Char token [80];
Char * P, * q;
Cout <"enter a sentence :";
Gets (STR );
P = STR;
While (* P) // extract one character from the string each time until * P = '/0'
{
Q = token; // point Q to the beginning of a mark
While (* P! = ''& * P) // & * P
* Q ++ = * P ++; // Q ++; P ++;
If (* P) P ++; // skip Space, * P =''
* Q = '/0'; // end with an empty character
Cout <token <"/N =" <Endl;
}
Return 0;
}
// -- Program #6. Mark extraction program: array subscript version
# Include <iostream>
# Include <cstdio>
Using namespace STD;
Int main ()
{
Char STR [40];
Char token [80];
Int I, J;
Cout <"enter a sentence :";
Gets (STR );
For (I = 0; I ++)
{
For (j = 0; STR [I]! = ''& STR [I]; j ++, I ++)
Token [J] = STR [I];
Token [J] = '/0 ';
Cout <token <"=" <Endl;
If (! STR [I])
Break;
}
Return 0;
}
// -- Program #7 use subscript for pointer like Array
# Include <iostream>
# Include <cctype>
Using namespace STD;
Int main ()
{
Char STR [20] = "Hello Tom ";
Char * P;
Int I;
P = STR;
For (I = 0; P [I]; I ++) // note the P [I]
P [I] = toupper (P [I]);
Cout <p <Endl;
Return 0;
}