c++指针的坑

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

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

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

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

如何理解1.呢?

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];

先看个错误代码:

// 错误代码
#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);

如下代码:

#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]));

再看正确的代码:

#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;
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值