何时将指针作为参数传递给C ++中的函数?

时间:2014-03-12 08:19:04

标签: c++ pointers arguments

Here我读了一篇关于何时将指针作为函数参数传递的文章。但我想知道一些情况,你应该将指针作为函数的参数传递给指针。 为了更清楚,我想知道何时应该使用类似的东西:

func(int **x);

6 个答案:

答案 0 :(得分:3)

C ++

在C ++中,您可以通过引用传递,并且当您希望修改参数以影响调用者传入的参数时,您可以这样做。也就是说,通过引用传递表示out或in / out参数。

如果函数需要指针(显然),或者如果要表示可选的输出参数,则传递指针 - 因为引用总是必须绑定到某个东西,但指针可以为null。

通过相同的逻辑,如果函数实际上需要双指针(在C ++中非常罕见)或者如果要表示指针类型的可选[in-] out参数(也非常罕见),则将指针传递给指针。 / p>

以下是一些例子(人为的,但应该证明这一点):

int square(int x)  //pass int by value
{ return x * x; }

void squareRoots(double in, double &plus, double &minus)  //pass double by reference
{
  plus = sqrt(in);
  minus = -plus;
}

int toNumber(String s, bool *ok = nullptr)  //optional out parameter
{
  try {
    int val = s.toNumber();
    if (ok)
      *ok = true;
    return val;
  } catch (NotANumber &) {
    if (ok)
      *ok = false;
    return 0;
  }
}

int computeAge(Person *person)  //pass pointer by value
{
  if (!person)
    return -1;
  else
    return now() - person->dateOfBirth();
}

bool createPerson(Person * &person)  //pass pointer by reference
{
  if (tooManyPeople())
    return false;
  person = new Person();
  return true;
}

int sum(int **values)  //pass double pointer by value
{
  if (!values)
    return 0;
  int res = 0;
  while (*values) {
    res += **values;
    ++values;
  }
  return res;
}

bool allocate(int ** &arr)  //pass double pointer by reference
{
  if (!enoughMemory())
    return false;
  arr = new int*[1024];  // array of 1024 pointers to int
  return true;
}

bool newNode(Node **node = nullptr)  //optional out parameter
{
  Node *added = dataStructure.createNode();
  if (node)
    *node = added;
  return added;
}

(注1:我这里只讨论非const引用,因为它与指针传递相关而不是引用传递。通过const-reference传递通常意味着“对象太大而无法复制”,并且当涉及到指针的指针时,它并不适用。)

(注2:上面的例子非常可怕,因为他们使用拥有原始指针,动态数组分配等。在实际的C ++中,你会使用智能指针,std::vector等。这就是指针的指针是在C ++中很少见。)


C

在C中,双指针更常见,因为C没有引用类型。因此,您还使用指向“按引用传递”的指针。如果在上面的C ++示例中的参数类型中使用&,则{C}中将使用*(并在操作参数时取消引用)。一个例子:

void squareRoots(double in, double *plus, double *minus)  //pass double "by reference"
{
  *plus = sqrt(in);
  *minus = -*plus;
}

答案 1 :(得分:2)

void SomeFun1(int *);
void SomeFun2(int **);
int i;
int *ptr = &i;

//when you want value of ptr should remain unchanged, but you want to change only value of i, use,
SomeFun1(int *)

//when you want value of ptr should change. i.e, it should point to some other memory other than i, use,
SomeFun2(int **);

答案 2 :(得分:2)

当您希望函数设置指针的值时,将指针作为参数传递给指针。

当函数想要分配内存(通过malloc或new)并在参数中设置该值时,通常会执行此操作 - 然后调用者将负责释放它。它比将其作为函数返回值返回更好,因为调用者不会错误地忽略返回值并且不会释放它而导致泄漏。

如果要返回多个值,也可能需要使用此选项。

执行此操作的另一个原因是,您希望可选地返回值(即指向指针的指针可以为NULL)。例如,看看strtol()它有一个可选的endptr。

注意:永远不要将此指针设置为堆栈变量。

答案 3 :(得分:1)

没有提到的是动态分配2D数组 - 实际上,你会有一个指向其他指针的指针数组。

答案 4 :(得分:1)

  • 如果要传递指针数组,可以使用<type>**

示例:

int main(int argc, char **argv) {   // argv is an array of char*
  • 如果您希望能够更改指针地址,您也可以使用它(在C ++中,您可以在这种情况下传递指针引用)。

答案 5 :(得分:0)

这也许是我的意见。但是,如果您希望将NULL用作完美值(例如,在链表中),则应将指针用于变量。 如果您不希望将值初始化为NULL,则应使用引用。 否则,引用和指针在很多方面类似。您可以从函数内部更改指针或引用参数的值。它们都是多态的。 ...