简体   繁体   English

无法在C函数中使用int *的值

[英]Unable to use the value of int * in c function

I'm having some trouble while trying to use the value of a int variable inside a function; 尝试在函数中使用int变量的值时遇到了一些麻烦; a variable that I need to change inside the function, so I passed it as a int * variable, and when calling the function simply passing the address of that variable like functionName(&variable); 我需要在函数内部更改的变量,因此我将其作为int *变量传递,并且在调用函数时仅传递了该变量的地址,例如functionName(&variable);。

But i'm having problems, and I have no idea of why i'm getting error while trying to compile it. 但是我遇到了问题,我也不知道为什么在尝试编译时会出错。 (I'm using debian 9) (我正在使用debian 9)

my function is here: 我的功能在这里:

void subvetcont(int * vetor, int tam, int * vetresult, int * tamresult){
  int incmaior=0;
  int fimmaior=0;
  int incaux=incmaior;
  int fimaux=fimmaior;

  *tamresult = 0;

  for(int i=1; i < tam; i++){
      if(vetor[i] == vetor[i-1]+1) fimaux=i;
      else if((fimaux-incaux) > (fimmaior - incmaior)){
          incmaior = incaux;
          fimmaior = fimaux;
          incaux=i;
          fimaux=i;
      }
      else{
        incaux = i;
        fimaux = i;
      }
  }
  for(int i=incmaior, tamresult=0; i <= fimmaior; i++){
      vetresult[(*tamresult)] = vetor[i];
      (*tamresult) = (*tamresult) + 1;
  }
}

what I got is 我得到的是

ex4.c: In function ‘subvetcont’:
ex4.c:104:14: error: invalid type argument of unary ‘*’ (have ‘int’)
   vetresult[(*tamresult)] = vetor[i];
              ^~~~~~~~~~
ex4.c:105:4: error: invalid type argument of unary ‘*’ (have ‘int’)
   (*tamresult) = (*tamresult) + 1;
                   ^~~~~~~~~~
ex4.c:105:19: error: invalid type argument of unary ‘*’ (have ‘int’)
   (*tamresult) = (*tamresult) + 1;
                   ^~~~~~~~~~

Am I using pointers wrong? 我使用的指针错误吗?

You've declared a new variable tamresult in the for loop: 您已经在for循环中声明了一个新变量tamresult

for (int i = incmaior = 0, tamresult = 0; i < fimmaior; i++) {
    ...
}

Within the loop, tamresult is that int variable, not the int* parameter to the function, so you can't indirect through it. 在循环内, tamresult是该int变量,而不是该函数的int*参数,因此您不能通过它间接访问。

I'm not sure why you put that there. 我不确定你为什么把那放在那里。 You already assigned *tamresult = 0; 您已经分配了*tamresult = 0; earlier in the function, there's no need to assign to it in the for header. 在函数的早期,无需在for标头中为其分配。 Just remove it. 只需将其删除。

for (int i = incmaior = 0; i < fimmaior; i++) {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM