c - 指针未更新

标签 c function pointers

#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?
...

调用foox会更新吗?

关于c - 指针未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44634368/

相关文章:

c - 在 c 中读取 uart,仅通过声明 char 数组是垃圾

c - 查看扩展的 C 宏

c++ - 参数列表中的 C 函数声明

javascript - 函数和 if else 语句不起作用

python XMLRPC函数错误

c++ - 从整数中减去指针

android - 直接从 Delphi 调用 Android NDK 函数的困难

c++ - C 与 C++ 中的三元运算符

c - 地址未堆叠、分配或(最近)释放

c++ - "possibly-hypothetical"在指针算术规则中意味着什么?