Professional Documents
Culture Documents
int *p;
listing 2
float *p;
listing 3
int *ip; // pointer to integers
listing 4
balptr = &balance;
listing 5
value = *balptr;
listing 6
#include <iostream>
using namespace std;
int main()
{
int balance;
int *balptr;
int value;
balance = 3200;
balptr = &balance;
value = *balptr;
cout << "balance is: " << value << '\n';
return 0;
}
listing 7
int *p;
double f;
// ...
p = &f; // ERROR
listing 8
int *p ;
double f;
// ...
p = (int *) &f; // Now technically OK
listing 9
// This program will not work right.
#include <iostream>
using namespace std;
int main()
{
double x, y;
int *p;
x = 123.23;
p = (int *) &x; // use cast to assign double * to int *
y = *p; // What will this do?
cout << y; // What will this print?
return 0;
}
listing 10
*p = 101;
listing 11
(*p)++;
listing 12
#include <iostream>
using namespace std;
int main()
{
int *p, num;
p = #
*p = 100;
cout << num << ' ';
(*p)++;
cout << num << ' ';
(*p)--;
cout << num << '\n';
return 0;
}
listing 13
p1++;
listing 14
p1--;
listing 15
p1 = p1 + 9;
listing 16
#include <iostream>
using namespace std;
int main()
{
int *i, j[10];
double *f, g[10];
int x;
i = j;
f = g;
return 0;
}
listing 17
char str[80];
char *p1;
p1 = str;
listing 18
str[4]
listing 20
// Tokenizing program: pointer version.
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[80];
char token[80];
char *p, *q;
p = str;
return 0;
}
listing 21
// Tokenizing program: array-indexing version.
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[80];
char token[80];
int i, j;
return 0;
}
listing 22
// Indexing a pointer like an array.
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char str[20] = "hello tom";
char *p;
int i;
p = str;
// index a pointer
for(i=0; p[i]; i++) p[i] = toupper(p[i]);
cout << p; // display the string
return 0;
}
listing 23
int num[10];
int i;
listing 24
*(num+3) = 100; // This is OK because num is not changed
listing 25
cout << strlen("C++ Compiler");
listing 26
#include <iostream>
using namespace std;
int main()
{
char *s;
cout << s;
return 0;
}
listing 27
#include <iostream>
using namespace std;
int main()
{
int num[10];
int *start, *end;
start = num;
end = &num[9];
return 0;
}
listing 28
int *pi[10];
listing 29
int var;
pi[2] = &var;
listing 30
*pi[2]
listing 31
char *fortunes[] = {
"Soon, you will come into 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"
};
listing 32
cout << fortunes[1];
listing 33
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
char *fortunes[] = {
"Soon, you will come into 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;
chance = rand();
chance = chance % 5;
cout << fortunes[chance];
return 0;
}
listing 34
// A simple C++ keyword synopsis program.
#include <iostream>
#include <cstring>
using namespace std;
char *keyword[][2] = {
"for", "for(initialization; condition; increment)",
"if", "if(condition) ... else ...",
"switch", "switch(value) { case-list }",
"while", "while(condition) ...",
// add the rest of the C++ keywords here
"", "" // terminate the list with nulls
};
int main()
{
char str[80];
int i;
// display syntax
for(i=0; *keyword[i][0]; i++)
if(!strcmp(keyword[i][0], str))
cout << keyword[i][1];
return 0;
}
listing 35
float *p = 0; // p is now a null pointer
listing 36
if(p) // succeeds if p is not null
listing 37
int **balance;
listing 38
// Multiple indirection.
#include <iostream>
using namespace std;
int main()
{
int x, *p, **q;
x = 10;
p = &x;
q = &p;
return 0;
}
listing 39
// This program is wrong.
int main(){
int x, *p;
x = 10;
*p = x; // where does p point?
return 0;
}
listing 40
char s[80];
char y[80];
char *p1, *p2;
p1 = s;
p2 = y;
if(p1 < p2) . . .
listing 41
int first[10];
int second[10];
int *p, t;
p = first;
for(t=0; t<20; ++t) {
*p = t;
p++;
}
listing 42
// This program is wrong.
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char s[80];
char *p1;
p1 = s;
do {
cout << "Enter a string: ";
gets(p1); // read a string
// print the ASCII values of each character
while(*p1) cout << (int) *p1++ << ' ';
cout << '\n';
} while(strcmp(s, "done"));
return 0;
}
listing 43
// This program is correct.
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char s[80];
char *p1;
do {
p1 = s; // reset p1 each time through the loop
return 0;
}