c/c++面试30-38之指针

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

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     
 6     int a[5] = { 1, 2, 3, 4, 5 };
 7     int *ptr = (int*)(&a + 1);//这里要特别注意,&a+1的值 &a+5*sizeof(int)也就是a[5]的地址 但是越界了 强制转换了int*
 8 
 9 
10     printf("%d
", *(a + 1));//2
11     printf("%d
", *(ptr - 1));//5
12     getchar();
13     return 1;
14 }

31 指针的比较

 1 #include <iostream>
 2 using namespace std;
 3 int main(void)
 4 {
 5     char str1[] = "abc";
 6     char str2[] = "abc";
 7     const char str3[] = "abc";
 8     const char str4[] = "abc";
 9     const char* str5 = "abc";
10     const char* str6 = "abc";
11     char* str7 = "abc";
12     char* str8 = "abc";
13 
14     //栈上分配 但是位置不同 所以为0
15     cout << (str1 == str2) << endl;//0 
16     cout << (str3 == str4) << endl;//0
17     //栈中分配  存放在数据区 所以是指向同一块数据区的内存 1
18     cout << (str5 == str6) << endl;//1
19     cout << (str7 == str8) << endl;//1
20     
21     getchar();
22     return 1;
23 }

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

(1)规则

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

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

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

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

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

(2)代码

 1 #include <stdio.h>
 2 
 3 int main10()
 4 {
 5     //两者都不可以对指向的内存进行修改 node1会出现编译错误 node2会出现运行错误
 6     const char* node1 = "abc";//常量指针
 7     char *const node2 = "abc";//指针常量
 8 
 9     /*node1[2] = 'k';
10     *node1[2] = 'k';
11     *node1 = "xyz";出现编译错误*/
12     node1 = "xyz";
13 
14     node2[2] = 'k';
15     /* *node2[2] = 'k';
16     *node2 = "xyz";运行错误*/
17     node2 = "xyz";
18 
19     getchar();
20     return 0;
21 }
View Code

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

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

  int *a[10]

(2)数组指针

  int *b = new int[10];

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

(3)代码

 1 #include<iostream>
 2 using namespace std;
 3 int main11()
 4 {
 5     int x1[4] = { 1, 2, 3, 4 };
 6     int x2[2] = { 5, 6 };
 7     int x3[3] = { 7, 8, 9 };
 8 
 9     int *a[2];//指针数组a 两个指针指向数组x2 x3
10     int *b = x1;//数组指针b 指向数组x1
11     int i = 0;
12 
13     a[0] = x2;
14     a[1] = x3;
15 
16     cout << "输出a[0]" << endl;
17     for (int i = 0; i < sizeof(x2) / sizeof(int); i++)
18     {
19         cout << a[0][i] << " ";
20     }
21     cout << endl;//5 6
22 
23     cout << "输出a[1]";
24     for (int i = 0; i < sizeof(x2) / sizeof(int); i++)
25     {
26         cout << a[1][i] << " ";//7 8 9
27     }
28     cout << endl;
29 
30 
31     cout << "b";
32     for (int i = 0; i < sizeof(x2) / sizeof(int); i++)
33     {
34         cout << b[i]<< " ";//1 2 3
35     }
36     cout << endl;
37 
38     getchar();
39     return 1;
40 }
View Code

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

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

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

(3)代码

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int max(int x, int y)
 6 {
 7     return (x > y ? x : y);
 8 }
 9 
10 float *find(float *p, int x)
11 {
12     return p + x;
13 }
14 
15 int main()
16 {
17     float score[] = { 10, 20, 30, 40 };
18     int(*p)(int, int);
19     float *q = find(score + 1, 1);
20     int a;
21 
22     p = max;
23     a = (*p)(1, 2);
24 
25     cout << "a=" << a << endl;//2
26     cout << "*q=" << *q << endl;//30
27     getchar();
28     return 0;
29 }
View Code

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

原文地址:https://www.cnblogs.com/lanjianhappy/p/7992438.html