请教一个函数与指针的问题
c语言吧
全部回复
仅看楼主
level 1
Tracycui49 楼主
假设a数组中的数据已按照有大到小的顺序存放,编写函数test1把a数组中相同的数据删的只剩一个,在主函数输出a数组中的数据。
int test1(int *a,int n)
这是我写的:
#include "stdio.h"
#include "stdlib.h"
#define N 5
int test1(int *a,int n)
{int i,j;
for(i=N-1;i>=0;i--)
{ if(*(a+i)==*(a+i-1))
{for(j=i;j<=n;j++)
*(a+j-1)=*(a+j);
n--;}}}
void main()
{int test1(int *a,int n);
int a[N],i,j,p,n;
for(i=0;i<N;i++)
scanf("%d",a+i);
n=i=N-1;
test1(a,n);
for(i=1;i<=n+1;i++)
{printf("%4d",*(a+i-1));}
system("pause");}
为什么结果是输出了N个数呢?导致最后一个数重复输出
2016年05月14日 01点05分 1
1