我是靠谱客的博主 无限枕头,这篇文章主要介绍c++指针的坑,现在分享给大家,希望可以做个参考。

总结:
一、使用指针的步骤:

  1. 声明
  2. 分配内存
  3. 初始化

二、对指针的的初始化只有两种方式:

  1. 把一个分配了内存的地址赋给他
  2. 用malloc给他分配一个内存,然后初始化

如何理解1.呢?

复制代码
1
2
3
4
5
6
7
8
9
char buffer[100]; strcpy(buffer, "aaa"); //buffer="aaa"是错误的;如果是char *buffer; buffer="aaa"则正确;虽然char *buffer;并没有给buffer分配内存,但是字符串常量“aaa”就相当于一个分配了内存的地址。 char *buf[50]; buf[0] = buffer; //把分配了内存的地址buffer赋给buf[0],所以不需要给buf[0]分配内存。 cout<<buf[0];

先看个错误代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 错误代码 #include <iostream> #include <fstream> #include <string.h> #include <stdio.h> using namespace std; int main() { char buffer[100]; char *buf[50]; strcpy(buffer, "helloooo"); strcat(buf[0], buffer); strcpy(buffer, "hellooo"); strcat(buf[1], buffer); strcpy(buffer, "helloo"); strcat(buf[2], buffer); strcpy(buffer, "hell"); strcat(buf[3], buffer); cout<<buf[0]<<endl; cout<<buf[1]<<endl; cout<<buf[2]<<endl; } // 发现什么也不输出!!

先来看char buffer[100],意思是声明一个字符数组buffer,同时对buffer分配100字节的内存;那么对于buffer就可以直接赋值了。

再看char *buf[50],意思是声明一个指针字符数组,50的意思是声明了50个指针(每个指针指向一个字符数组),注意并不是分配的内存,所以此时并不能对buf[0],buf[1]…初始化,因为还没有对他们各自分配内存。

如果想对他们初始化,需要进行分配内存:
buf[0] = (char *) malloc(100);

如下代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream> #include <fstream> #include <string.h> #include <stdio.h> using namespace std; int main() { char buffer[100]; char *buf[50]; buf[0] = (char *) malloc(100); cout<<buf[0]<<endl; strcpy(buffer, "helloooo"); strcat(buf[0], buffer); buf[1] = (char *) malloc(100); cout<<buf[1]<<endl; strcpy(buffer, "hellooo"); strcat(buf[1], buffer); buf[2] = (char *) malloc(100); strcpy(buffer, "helloo"); strcat(buf[2], buffer); buf[3] = (char *) malloc(100); strcpy(buffer, "hell"); strcat(buf[3], buffer); cout<<buf[0]<<endl; cout<<buf[1]<<endl; cout<<buf[2]<<endl; }

然而,看输出:
在这里插入图片描述

这是怎么回事呢?
从输出内容会发现,分配的内存里面有乱七八糟的内容!另外,我们用的是strcat(buf[0], buffer),他的意思是将buffer的内容追加到buf[0]后面,那么输出自然会带有乱七八糟的内容!!当然如果你用strcpy(buf[0], buffer)就可以避免这种情况,因为strcpy是覆盖。

一般来说系统给你分配的内存里面应该是空的,只有少数情况下运气比较差,给你分配的内存里面原来就存有东西,这时候用memset把它初始化一下就行了:
memset(buf[0], 0, sizeof(buf[0]));

再看正确的代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream> #include <fstream> #include <string.h> #include <stdio.h> using namespace std; int main() { char buffer[100]; char *buf[50]; cout<<"----------n"; buf[0] = (char *) malloc(100); memset(buf[0], 0, sizeof(buf[0])); cout<<buf[0]<<endl; strcpy(buffer, "helloooo"); strcat(buf[0], buffer); buf[1] = (char *) malloc(100); memset(buf[1], 0, sizeof(buf[0])); cout<<buf[1]<<endl; strcpy(buffer, "hellooo"); strcat(buf[1], buffer); buf[2] = (char *) malloc(100); memset(buf[2], 0, sizeof(buf[0])); strcpy(buffer, "helloo"); strcat(buf[2], buffer); buf[3] = (char *) malloc(100); memset(buf[3], 0, sizeof(buf[0])); strcpy(buffer, "hell"); strcat(buf[3], buffer); cout<<buf[0]<<endl; cout<<buf[1]<<endl; cout<<buf[2]<<endl; }

输出:
在这里插入图片描述

最后

以上就是无限枕头最近收集整理的关于c++指针的坑的全部内容,更多相关c++指针内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部