我做了一个程序来删除数组中的重复项,但程序的条件总是保持为真。 我明白了问题所在,将arr [i]更改为arr [count]并通过malloc分配内存,但程序按原样打印数组而不删除重复项。删除数组中的重复项
# include<stdio.h>
# include<stdlib.h>
int count=0;
void Delete(int *arr);
void Search(int *arr);
int main()
{
int i;
int *arr;
arr=(int*)malloc(sizeof(int));
clrscr();
printf("Enter array and press -1 to stop:\n");/*stops when -1 occurs*/
for(count=0; ;count++)/*count is the count of the numbers*/
{
scanf("%d",&arr[count]);
realloc(arr,sizeof((int)+count));
fflush(stdin);
if(*(arr+count)==-1)/*This condition is never true.*/
break;
}
Search(arr);
for(i=0;i<count;i++)
{
printf("%d\t",arr[i]);
}
getch();
return 0;
}
Search(arr);
for(i=0;i<count;i++)
{
printf("%d",&arr[i]);
}
getch();
return 0;
}
是什么这个意思是:if(*(arr + count)== - 1) - 为什么不使用arr [count] == -1?你的代码乱丢这个... – 2010-10-02 20:17:29
change scanf(“%d”,&arr [i]);对scanf(“%d”,&arr [count]); ; 编辑:btw这是作业,不是吗? – George 2010-10-02 20:18:20
保持数组长度在一个全局变量count中是有问题的。 – Arun 2010-10-02 20:34:17