首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

真不知是哪错啦啊该如何解决

2013-10-12 
真不知是哪错啦啊。。。。。#include stdio.h/* 输入 */void input (int n,int * p){int * p1p1 pwhile (

真不知是哪错啦啊。。。。。

#include <stdio.h>
/* 输入 */
void input (int n,int * p)
{
int * p1;
p1 = p;
while (p < (p1 + n))
{
scanf_s ("%d",p);
p ++;
}
}
/* 计算 */
void count (int n , int  * p)
{
int max , min , temp ,* pmax , * pmin , * p1;
p1 = p;
max = min = * p;
while ( p < p1 + n )
{
if ( * p > max )
{
max = * p;
pmax = p;
}
p ++;
}
temp = * pmax;
* pmax = * ( p - 1 );
* ( p - 1 ) = temp;
p = p1;
while ( p < p1 + n )
{
if ( * p < min )
{
min = * p;
pmin = p;
}
p ++;
}
temp = * pmin;
* pmin = * ( p - n );
* ( p - n ) = temp;
}
/* 输出 */
void output ( int n , int  * p )
{
int * p1;
p1 = p;
while ( p < p1 + n )
{
printf_s ( "%4d" , *p );
p ++;
}
}
int main ( void )
{
int a [ 10 ];
input ( 10 , a );
count ( 10 , a );
output ( 10 , a );
}


运行时,输入1 2 3 4 5 6 7 8 9 0 程序能正常运行,结果也对!
但是如果输入9 0 1 2 3 4 5 6 7 8 运行就会Debug Error!
为什么会这样?

[解决办法]
max = min = * p;
while ( p < p1 + n )
{
if ( * p > max )
{
max = * p;
pmax = p;
}
p ++;
}
temp = * pmax;

人脑执行,输入为 9 0 1 2 3 4 5 6 7 8的时候*p = 9 max =9,然后你这个if 永远不会进入也就是*p > max不会满足,pmax指针为野指针,到*pmax就出错,将if (*p > max)修改为if (*p >= max)

热点排行