C++指针作为函数参数(详解版)

  • 内容
  • 评论
  • 相关

《函数引用传递》一节,我们介绍了用作函数形参的引用变量,引用变量其实就是原始变量的别名,该原始变量用作实参。这样的设计使得函数可以访问原始实参变量,从而允许它更改变量的内容。当为一个变量传入一个引用形参时,则认为该实参是按引用传递的。

按引用传递实参还有一种替代方法,那就是使用指针作为函数参数。无可否认,引用变量比指针更容易处理,因为引用变量隐藏了所有的解引用和间接引用"机制"。但是,程序员仍然应该学会使用指针作为函数参数,因为有一些任务,特别是在处理 C 字符串时,最好使用指针完成、另外,C++ 库中还有许多使用指针作为形参的函数。

以下是使用指针形参的函数的定义:

void doubleValue(int *val)
{
    *val *= 2;
}

这个函数的目的是使 val 指向的变量翻倍。当 val 被解引用时,*= 运算符对 val 指向的变量起作用。该语句可以将地址存储在 val 中的原始变量乘以 2。当然,当调用该函数时,必须使用被翻倍的变量地址作为实参,而不是变量本身作为实参。

以下是一个调用 doubleValue 函数的示例:

doubleValue(&number);

该语句使用了地址运算符(&)将 number 的地址传递到 val 形参中。函数执行后,number 的内容将被乘以 2。下面的程序演示了该函数的用法:

//This program uses two functions that accept addresses of variables as arguments.
#include <iostream>
using namespace std;
//Function prototypes
void getNumber(int *);
void doubleValue(int *);

int main()
{
    int number;
    //Call getNumber and pass the address of number
    getNumber(&number);
    // Call doubleValue and pass the address of number
    doubleValue(&number);
    // Display the value in number
    cout << "That value doubled is " << number << endl;
    return 0;
}
void getNumber(int *input)
{
    cout << "Enter an integer number: ";
    cin >> *input;
}

void doubleValue(int *val)
{
    *val *= 2;
}

程序输出结果:

Enter an integer number: 10
That value doubled is 20

此程序有两个使用指针作为参数的函数。请看以下函数原型:

void getNumber(int *);
void doubleValue(int *);

每一个函数原型都使用符号 int * 来表示该形参是一个指向 int 的指针。与所有其他类型的形参一样,不需要在原型中指定变量的名称,但星号(*)则是必需。

getNumber 函数要求用户输入一个整数值。以下 cin 语句可以将用户输入的值存储在内存中:

cin >> *input;

间接运算符会使用户输入的值存储在 input 指向的变量中,而不是 input 中。

在上面的语句中,使用间接运算符是非常重要的。没有它,则 cin 会将用户输入的值存储在 input 中,就像该值是一个内存地址一样。如果发生这种情况,则 input 将不再指向 main 函数中的 number 变量。如此一来,对该指针(input)的后续使用即使不会产生灾难性的结果,也必然出现错误。

当调用 getNumber 函数时,函数 main 中 number 变量的地址作为实参传递。该函数执行后,用户输入的数值将被存储在 number 中。接下来,调用 doubleValue 函数,同样是将 number 的地址作为实参传递,这使得 number 被乘以 2。

指针变量也可以用来接收数组地址作为实参,此后,无论是下标还是指针符号都可以用来处理数组的内容。下面的程序演示了这一点:

//This program demonstrates that a pointer may be used as a parameter to accept the address of an array. Either subscript or pointer notation may be used.
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
void getSales(double *sales, int size);
double totalSales(double *sales, int size);

int main()
{
    const int QUARTERS = 4;
    double sales[QUARTERS];

    getSales(sales, QUARTERS);
    cout << setprecision(2);
    cout << fixed << showpoint;
    cout << "The total sales for the year are $";
    cout << totalSales(sales, QUARTERS) << endl;
    return 0;
}
void getSales(double *array, int size)
{
    for (int count = 0; count < size; count++)
    {
        cout << "Enter the sales figure for quarter ";
        cout << (count + 1) << ": ";
        cin >> array[count];
    }
}

double totalSales(double *array, int size)
{
    double sum = 0.0;
    for (int count = 0; count < size; count++)
    {
        sum += *array;
        array++;
    }
    return sum;
}

程序输出结果:

Enter the sales figure for quarter 1: 10263.98
Enter the sales figure for quarter 2: 12369.69
Enter the sales figure for quarter 3: 11542.13
Enter the sales figure for quarter 4: 14792.06
The total sales for the year are $48967.86

请注意,该程序的 getSales 函数中,即使把形参 array 定义为一个指针,其下标符号仍然可以在 cin 语句中使用:

cin >> array[count];

在 totalSales 函数中,array 还可以与以下语句中的间接运算符一起使用:

sum += *array;

而在接下来的语句中,array 中的地址则可以递增,以使指向下一个元素:

array++;

上面介绍的两个语句也可以合并成以下语句:

sum += *array++;

* 运算符将首先解引用 array,然后 ++ 运算符将使得 array 中的地址递增。

本文标题:C++指针作为函数参数(详解版)

本文地址:https://www.hosteonscn.com/3830.html

评论

0条评论

发表评论

邮箱地址不会被公开。 必填项已用*标注