在函数bag的for循环体中数组越界了。 还不单单是这个问题呢,楼主怎么用p做循环变量了呀???? 第17行引出的错误,因为for (p = 0;*p <= 10; p++)
这里的p 是一个地址,你当然不能p=0, 把数组当参数来传的话,最好把数组的长度传过来,
#include <iostream>
using namespace std;
int max;
int min;
void main()
{
void bag(int *p, int n);
void bag(int *p); int number = {0};
cout << "input 10 numbers" << endl;
for (int i = 0; i <= 9; i++)
{
cin >> *(number+i);
}
bag(number, 10);
cout << "max=" << max << endl;
cout << "min=" << min << endl;
}
void bag(int *p, int n)
{
int i = 0;
for (;i < n; p++,i++)
{
if (max < *p)
{
max= *p;
}
if (min > *p)
{
min = *p;
}
}
} 上面个程序有点小问题
#include <iostream>
using namespace std;
int max;
int min;
void main()
{
void bag(int *p, int n);
void bag(int *p); int number = {0};
cout << "input 10 numbers" << endl;
for (int i = 0; i <= 9; i++)
{
cin >> *(number+i);
}
bag(number, 10);
cout << "max=" << max << endl;
cout << "min=" << min << endl;
}
void bag(int *p, int n)
{
int i = 0;
for (;i < n; p++,i++)
{
if(i == 0) {
max = min = *p;
}
if (max < *p)
{
max= *p;
}
if (min > *p)
{
min = *p;
}
}
}
页:
[1]