#include <stdio.h>
#include <stdlib.h>
int* removeNegatives(int *v, int *totv){
int i, j, aux=(*totv), t=0;
for(i=0; i<aux; i++){
if(v[i]<0){
t=v[i];
for(j=i; j<=aux; j++){
v[j]=v[j+1];
}
v[(*totv)-1]=t;
aux--;
i=-1;
}
else{
continue;
}
}
totv=&aux;
v=(int*)realloc(v,(*totv)*sizeof(int));
return(v);
}
int main(){
int *totv=NULL, *v=NULL, *z=NULL, i, j=0, a;
printf("How many numbers are you entering?\n");
scanf("%d",&i);
printf("Enter them, then:\n");
totv=&i;
do{
if(j<(*totv)){
scanf("%d",&a);
v=(int*)realloc(v,++j*sizeof(int));
v[j-1]=a;
}
}while(j<(*totv));
printf("\n");
printf("Size before: %d\n",*totv);
z=retiraNegativos(v,totv);
printf("Size after: %d\n",*totv);
printf("\n");
printf("[ ");
for(i=0; i<(*totv); i++){
printf("%d ",z[i]);
}
printf("]");
printf("\n");
free(z);
return(0);
}
我正在根据用户输入的负数数量调整 vector “v”的大小。
但问题是,在我调用函数“removeNegatives”后,指针“totv”没有更新。
非常感谢您的帮助!
最佳答案
这可能看起来与您的问题无关,但事实并非如此。
考虑一下:
int foo(int bar)
{
bar = 123;
}
...
int x = 1;
foo(x);
// What's the value of x here?
...
调用foo
后x
会更新吗?
关于c - 指针未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44634368/