我做了一個程序來刪除數組中的重複項,但程序的條件總是保持爲真。 我明白了問題所在,將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