C ++ shortcut tutorial-Chapter 6-pointer (Part 2)

Source: Internet
Author: User

// -- 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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.