Nhờ sửa lỗi Access violation

Lỗi:

Unhandled exception at 0x004960D6 in BTTT.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC

Code:

#include <iostream>
using namespace std;

int main()
{
	int  b[10] = { 2, 4, 7, 2, 1, 7, 3, 9, 3, 6 };
	int *a[10];

	for (int i = 0; i < 10; i++)
	{
		a[i] = &b[i]; // gan dia chi cua so nguyen.
	}
	for (int i = 0; i < 10; i++)
	{

		cout << *a[i] << " ";
	}
	for (int i = 0; i<10; i++)
	{
		int j = i + 1;
		if (*a[i]>*a[j])
		{
			swap(*a[i], *a[j]);
		}
	}
	cout << endl;
	for (int i = 0; i < 10; i++)
	{

		cout << b[i] << " ";
	}
	return 0;
}

Xem ra bạn truy xuất đến phần tử thứ 11 (index: 10) luôn rồi.

4 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?