《C++从入门到精通》实例--指针
//范例1:通过指针变量访问整型变量
//知识点:指针变量
//2018.3.18
#include <iostream>
using namespace std;
int main(int argc, int argv[])
{
int a = 1, b = 2;
int *p1, *p2;
p1 = &a;
p2 = &b;
cout << "a=" << a << " " << "b=" << b << endl;
cout << "a=" << *p1 << " " << "b=" << b << endl;
system("pause");
return 0;
}
//范例2:交换指针变量的地址
//知识点:指针变量
//2018.3.18
#include <iostream>
using namespace std;
int main(int agc,char* argv[])
{
int *p1, *p2,*p, a = 1, b = 2;
p1 = &a;
p2 = &b;
p = p1;
p1 = p2;
p2 = p;
cout << "a=" << a << " " << "b=" << b << endl;
cout << "a=" << *p1 << " " << "b=" << *p2 << endl;
system("pause");
return 0;
}
//范例3:使用函数实现对输入的两个整数按大小顺序输出
//知识点:指针变量
//2018.3.18
#include <iostream>
using namespace std;
void swap(int *p1, int *p2)
{
int temp;
temp = *p1; //把指针p1所指向的地址中的值暂存在temp中
*p1 = *p2; //把指针p2所指向的地址中的值存在p1指向的地址中
*p2 = temp; //把temp中值存储到p2所指向的地址中
}
int main(int argc,char* argv[])
{
int a = 1, b = 2;
int *point1, *point2;
point1 = &a;
point2 = &b;
cout << "交换前:a=" << a << " " << "b=" << b << endl;
swap(point1, point2);
cout << "交换后:a=" << a << " " << "b=" << b << endl;
system("pause");
return 0;
}
//范例3拓展1:修改swap代码
//知识点:指针变量
//2018.3.18
#include <iostream>
using namespace std;
void swap(int *p1, int *p2)
{
int *temp; //临时量,但类型为指针
temp = p1; //把指针p1的值暂存在temp中
p1 = p2; //把指针p2的值存在p1中
p2 = temp; //把temp中值存储到p2中
}
int main(int argc,char* argv[])
{
int a = 1, b = 2;
int *point1, *point2;
point1 = &a;
point2 = &b;
cout << "交换前:a=" << a << " " << "b=" << b << endl;
swap(point1, point2);
cout << "交换后:a=" << a << " " << "b=" << b << endl;
system("pause");
return 0;
}
//范例4:指针变量自身的运算
//知识点:
//2018.3.18
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int *p1, *p2, a = 1, b = 10;
p1 = &a;
p2 = &b;
cout << "p1地址中的值是" << *p1 << endl;
cout << "p2地址中的值是" << *p2 << endl;
cout << "p1-1地址中的值是" << *(p1-1) << endl;
cout << "p1中的值-1是" << *p1-1 << endl;
system("pause");
return 0;
}
PS:栈区和堆区内存分配的区别
//范例5:使用数组指针访问数组元素
//知识点:指针和一维数组
//2018.3.19
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int array[10];
int i, *p;
for (i = 0; i < 10; i++)
cin >> array[i];
cout << endl;
for (p = array; p < (array + 10); p++) //数组的名称是该数组的首地址,即首个元素的指针
cout << *p << " ";
cout << endl;
system("pause");
return 0;
}
//范例6:使用数组指针访问二维数组元素
//知识点:指针和多位数值数组
//2018.3.19
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int a[2][3] = { 1, 2, 3, 4, 5, 6 };
int *p, i, j;
p = a[0]; //p指向数组a的首地址
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
cout << *(p + 3 * i + j) << " "; //指针p逐步后移,访问数组每一个元素
}
cout << endl;
}
system("pause");
return 0;
}
//范例7:使用数组指针访问二维数组元素
//知识点:指针和多位数值数组
//2018.3.19
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int a[2][3] = { 1, 2, 3, 4, 5, 6 };
int (*p)[3], i, j; //p叫数组指针,它表示指向一个每行有3个元素的数组的首地址
p = a; //p指向数组a的首地址
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
cout << p[i][j] << " "; //指针p的每一个元素
}
cout << endl;
}
system("pause");
return 0;
}
/范例8:字符串复制函数功能实现方法
//知识点:指针和字符数组
//2018.3.20
#include <iostream>
using namespace std;
void copystr(char *str1, char *str2)
{
while (*str2 != 0)
{
*str1++ = *str2++;
}
*str1 = '\0';
}
int main(int argc, char* argv[])
{
char a[10];
char b[4] = "abc";
copystr(a, b);
cout << "a=" << a << endl;
system("pause");
return 0;
}
//范例9:指向指针的指针
//知识点:
//2018.3.20
#include <iostream>
using namespace std;
int main(int argc, char* argv)
{
char *arr[] = { "abc", "12345", "language" }; //初始化指针数组arr,每个指针都是一个字符指针
char **p;
int i;
for (i = 0; i < 3; i++)
{
p = arr + i; //指针p指向arr+i所指向的字符串的首地址
cout << *p << endl; //输出数组中的每一个字符串
}
system("pause");
return 0;
}
//范例10:指向函数的指针
//知识点:函数指针
//2018.3.21
#include <iostream>
using namespace std;
int max(int x, int y)
{
return x > y ? x : y;
}
int main(int argc, char* argv[])
{
int(*p)(int, int); //p是指向有两个整型参数函数的整型指针
int a, b, c;
p = max; //p指向函数max
cin >> a >> b;
c = (*p)(a, b);
cout << "a=" << a << "b=" << b << "c=" << c << endl;
system("pause");
return 0;
}
//范例11:返回指针的函数
//知识点:返回指针的函数
//2018.3.21
#include <iostream>
#include <string>
using namespace std;
char *max(char *x, char *y)
{
if (strcmp(x, y) > 0)
return x;
else
return y;
}
int main(int argc, char* argv[])
{
char c1[10], c2[10];
char *s1 = c1, *s2 = c2; //声明两个字符串指针,并对其初始化
cout << "请输入字符串1:" << endl;
cin >> c1;
cout << "请输入字符串2:" << endl;
cin >> c2;
cout << "两个字符串中较大的是:" << endl;
cout << max(s1, s2) << endl;
system("pause");
return 0;
//范例12:返回指针的函数和数组指针
//知识点:
//2018.3.21
#include <iostream>
using namespace std;
int *find(int(*p)[2], int num)
{
int *point;
point = *(p + num);
return point;
}
int main(int argc, char* argv[])
{
int value[3][2] = { { 70, 80 }, { 80, 90 }, { 90, 100 } };
int *p;
int num, i;
cout << "请输入要查找的序号:" << endl;
cin >> num;
p = find(value, num);//实参为value数组第0行的首地址和要查找的序号,返回第num行首地址
cout << "序号" << num << "的成绩分别是:" << endl;
for (i = 0; i < 2; i++)
{
cout << *(p + i) << endl; //依次输出第num行的每个元素
}
system("pause");
return 0;
}