数组会越界,但是指针不会……

rt:

但是越界的时候指针只能读,而不可以写,具体看看这个程序就一目了然了!

// e[i].cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char *a = "addf";
char h = a[60];

cout << h << endl;
a[60] = 5;
char e[5] = "adsf";
char i = e[60];
cout << i;
return 0;
}

再看看另外一种情况,就是给指针赋一个地址以后就不会出现这么个情况了

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char *a = "addf";
char h = a[60];

cout << h << endl;
//给定一个单一的地址单元
a = &h;
a[60] = 5;
//给定一个连续的但是貌似可以“越界的”地址单元
char b[3];
a = b;
a[60] = 5;
return 0;
}

上面的程序是没有问题的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值