#include "stdafx.h"
// Demo.cpp : 定义控制台应用程序的入口点。
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
//数组
#if 1
int main(void) {
int iBefore;
int iScore[10];
int iAfter;
int iScore2[10];
cout << "addr of iBefore:" << &iBefore << endl;
cout << "addr of iScore:" << iScore << endl; //数组名本身就是地址,不需要&
cout << "addr of iAfter:" << &iAfter << endl;
cout << "addr of iScore2:" << iScore2 << endl;
// iScore[i] ------- int
// iScore + i*sizeof(int) 不进行越界检查
for (int i = 0; i < 10; i++) {
iScore[i] = i;
iScore2[i] = 0;
}
for (int i = 0; i < 10; i++) {
cout << iScore[i];
}
cout << endl;
/*
iScore[11] = 9999;
cout << "iScore[11]" << iScore[11] << endl;
// 这时候会测得iScore和iScore2地址相差44个字节,即11个int 用Release版本运行
*/
// 绕过越界检查 要用Release版本,各定义变量地址紧挨一起
int idx;
cin >> idx; // 这里在命令行输入大于10的数
iScore[idx] = 9999;
cout << "iScore[idx]:" << iScore[idx] << endl;
cout << "contents of iScore2" << endl;
for (int i = 0; i < 10; i++) {
cout << iScore2[i];
}
cout << endl;
return 0;
}
#endif
3.1 数组及自定义数据类型 【C++】
猜你喜欢
转载自blog.csdn.net/weixin_44773006/article/details/103504011
今日推荐
周排行