30 看代码写结果-----指针加减

 #include <stdio.h>

 int main(void)
{ int a[] = { , , , , };
int *ptr = (int*)(&a + );//这里要特别注意,&a+1的值 &a+5*sizeof(int)也就是a[5]的地址 但是越界了 强制转换了int* printf("%d\n", *(a + ));//
printf("%d\n", *(ptr - ));//
getchar();
return ;
}

31 指针的比较

 #include <iostream>
using namespace std;
int main(void)
{
char str1[] = "abc";
char str2[] = "abc";
const char str3[] = "abc";
const char str4[] = "abc";
const char* str5 = "abc";
const char* str6 = "abc";
char* str7 = "abc";
char* str8 = "abc"; //栈上分配 但是位置不同 所以为0
cout << (str1 == str2) << endl;//0
cout << (str3 == str4) << endl;//0
//栈中分配 存放在数据区 所以是指向同一块数据区的内存 1
cout << (str5 == str6) << endl;//
cout << (str7 == str8) << endl;// getchar();
return ;
}

32 指针常量和常量指针的区别

(1)规则

  前面的一个通常式修饰部分,中心词式后面一个词

    常量指针:常量的指针,首先是一个指针

      指针指向的地址的内容式不可以修改的

    指针常量:指针的常量,它首先是一个常量,然后才是指针

      不能修改这个指针所指向的地址,只能指向那个位置,就好像一个数组的数组名一样,固定的指针(不能使用p++),但是指向的内容是可以改变的

(2)代码

 #include <stdio.h>

 int main10()
{
//两者都不可以对指向的内存进行修改 node1会出现编译错误 node2会出现运行错误
const char* node1 = "abc";//常量指针
char *const node2 = "abc";//指针常量 /*node1[2] = 'k';
*node1[2] = 'k';
*node1 = "xyz";出现编译错误*/
node1 = "xyz"; node2[] = 'k';
/* *node2[2] = 'k';
*node2 = "xyz";运行错误*/
node2 = "xyz"; getchar();
return ;
}

33 指针数组和数组指针的区别

(1)指针数组是:一个数组存放的都是同一个类型的指针

  int *a[10]

(2)数组指针

  int *b = new int[10];

  b指向含有10个整形数据的一维数组 释放一定要delete[]

(3)代码

 #include<iostream>
using namespace std;
int main11()
{
int x1[] = { , , , };
int x2[] = { , };
int x3[] = { , , }; int *a[];//指针数组a 两个指针指向数组x2 x3
int *b = x1;//数组指针b 指向数组x1
int i = ; a[] = x2;
a[] = x3; cout << "输出a[0]" << endl;
for (int i = ; i < sizeof(x2) / sizeof(int); i++)
{
cout << a[][i] << " ";
}
cout << endl;//5 6 cout << "输出a[1]";
for (int i = ; i < sizeof(x2) / sizeof(int); i++)
{
cout << a[][i] << " ";//7 8 9
}
cout << endl; cout << "b";
for (int i = ; i < sizeof(x2) / sizeof(int); i++)
{
cout << b[i]<< " ";//1 2 3
}
cout << endl; getchar();
return ;
}

34 函数指针与指针函数的区别

(1)每一个函数,本省都有一个入口地址,这个地址相当于一个指针。

(2)函数指针是指向函数的指针变量,所以本身就是一个指针变量,只不过指针变量指向函数。这样一来,有了指向函数的指针变量以后,我们就可以通过这个指针来调用函数。指针函数是返回指针类型的函数

(3)代码

 #include <iostream>

 using namespace std;

 int max(int x, int y)
{
return (x > y ? x : y);
} float *find(float *p, int x)
{
return p + x;
} int main()
{
float score[] = { , , , };
int(*p)(int, int);
float *q = find(score + , );
int a; p = max;
a = (*p)(, ); cout << "a=" << a << endl;//
cout << "*q=" << *q << endl;//
getchar();
return ;
}

35 数组指针和函数指针的定义

(1) 含有10个元素的指针数组  int *a[10]

(2) 数组指针 int *a = new int[10]

(3) 函数指针 void (*fn)(int,int)

(4) int (*fnArray[10])(int,int)

36 各种指针的定义

(1)void (*f)(int,int) f是指向void max(int x,int y)类型的函数指针

(2)int *fn() fn是返回int指针类型的函数

(3)const int *p p是一个指向const的指针,指向一个常量

(4)int* const q;q是一个const指针

(5)const int* const ptr ptr是一个指向const的const指针

37 typedef用于函数指针的定义

(1) typedef int (*pfun)(int x,int y)

(2)解析:

  pfun是一个使用typedef自定义的数据类型。它表示一个函数指针,其参数有两个,都是int类型,返回值也是int类型。

(3) 使用方法

  typedef int (*pfun)(int x,int y)//表示pfun是一个函数指针类型

  int fun(int x,int y) //定义一个返回值为int的函数

  pfun p = fun//函数指针p赋给fun的地址

  int ret  = p(2,3);//调用

38 什么是野指针

(1)野指针不是NULL指针,是指向“垃圾”内存的指针

(2)产生的原因

  a:指针变量没有初始化。默认是乱指的

  b:指针被free或者delete之后没有置为NULL

最新文章

  1. 使用TSQL查询和更新 JSON 数据
  2. GitHub 实现多人协同提交代码并且权限分组管理
  3. 贪心 HDOJ 4726 Kia&#39;s Calculation
  4. 判断浏览器是否为IE内核的最简单的方法
  5. linux 定时器编程实例(完善中).....
  6. mysql inner join,full outer join,left join,right jion
  7. phpcms:一、安装及新建模板
  8. 一键搞定Java桌面应用安装部署 —— exe4j + Inno Setup 带着JRE, 8M起飞
  9. 如何配置web服务器
  10. Zookeeper分享
  11. Jmeter脚本录制方法(二)——手工编写脚本(jmeter与fiddler结合使用)
  12. http 缓存学习.
  13. Jmeter学习——http请求Content encoding的重要性
  14. 赵雅智_Swift(4)_断言
  15. 判断input[type=file]上传文件格式
  16. SQL注入之Sqli-labs系列第二十五关(过滤 OR &amp; AND)和第二十五A关(过滤逻辑运算符注释符)
  17. Linux命令(三) 移动文件 mv
  18. innobackupex在线备份及恢复(全量和增量)
  19. Selenium常用操作汇总二——如何处理alert、confirm、prompt对话框
  20. TeX-换行换页与段落命令

热门文章

  1. Kubernetes对象之Pod
  2. 换站点Logo图片---轻开电子商务系统(企业入门级B2C站点)
  3. 用js判断文本框中的是不是空,是否有空格
  4. Html.DropDownListFor的选项值为字符型问题
  5. 监控hadoop任务结果shell脚本
  6. r testifying that your code will behave as you intend.
  7. request,session,application三者关系&lt;转&gt;
  8. MFC HTTP(S)请求笔记
  9. UVA 10951 - Polynomial GCD(数论)
  10. [自动化平台系列] - 初次使用 Macaca-前端自动化测试(3)