我怎样才能通过引用传递这个指针?
How can I pass this pointer by reference?
指针和引用是新手,所以我不太确定这一点,但我正在尝试传递指针 *minDataValue 和 *maxDataValue,以便在它们从函数返回时更改它们的值。截至目前,在代码中,它们的值没有改变(从测试代码可以看出),我如何设置它们以及我必须更改什么才能使它们通过引用传递以便值可以在函数时更改已经完成了。谢谢!
void findMinAndMax(int array[], int size, int *min, int *max) {
int smallest = array[0];
int largest = array[0];
min = &smallest;
max = &largest;
for (int i = 1; i < size; i++)
{
if (array[i] > largest){
largest = array[i];
}
if (array[i] < smallest){
smallest = array[i];
}
}
// testing code
cout << *min << endl;
cout << *max << endl;
}
int *makeFrequency (int data[], int dSize, int *minDataValue, int *maxDataValue) {
cout << *minDataValue << endl;// testing code
cout << *maxDataValue << endl;// testing code
findMinAndMax(data, dSize, minDataValue, maxDataValue); // How do I pass this so that the value changes after the min and max are found?
cout << *minDataValue << endl; // testing code
cout << *maxDataValue << endl;// testing code
}
int main() {
int dSize;
int *ArrayOfInts;
cout << "How many data values? ";
cin >> dSize;
ArrayOfInts = new int [dSize];
getData(dSize, ArrayOfInts);
int *frequency, min, max;
frequency = makeFrequency(ArrayOfInts, dSize, &min, &max);
}
您可以采用函数采用指向指针的指针:
int *makeFrequency (int data[], int dSize, int **minDataValue, int **maxDataValue)
然后修改
*minDataValue 和
*最大数据值
函数内部。在这种情况下,调用代码必须显式传递一个指向指针的指针:
int *min, *max;
makeFrequency (data, size, &min, &max)
第二种可能是传递对指针的引用:
int *makeFrequency (int data[], int dSize, int*& minDataValue, int *& maxDataValue)
在这种情况下,您只需修改函数内部的指针并将指针传递给函数即可。
我更喜欢第一种方式,因为这样调用代码看起来也不一样,所以指针可能在函数内部被修改的事实也从调用代码中可见。
You have to use pointer of pointer or pass by reference (@Sami Sallinen explained) to change the value *minDataValue and *maxDataValue inside the functions
//Using pointer of pointer
void findMinAndMax(int array[], int size, int **min, int **max)
{
int *smallest = &array[0];
int *largest = &array[0];
min = &smallest;
max = &largest;
for (int i = 1; i < size; i++)
{
if (array[i] > *largest){
*largest = array[i];
}
if (array[i] < *smallest){
*smallest = array[i];
}
}
// testing code
cout << **min << endl;
cout << **max << endl;
}
int *makeFrequency (int data[], int dSize, int *minDataValue, int *maxDataValue)
{
findMinAndMax(data, dSize, &minDataValue, &maxDataValue);
}
有两种实现方式。我会举一个涉及函数和指针的小例子。
void f1 (int** p) // argument is pointer to pointer
{
}
void f2 (int*& p) // argument is reference to pointer
{
}
int main()
{
int x = 5;
int* p = &x;
f1 (&p); // pass address of pointer
f2 (p); // pass reference (C++ type) of pointer
return 0;
}
与 f2
不同,f1
比 C++
更多 C
。但是我喜欢 f2
,因为我觉得它是 type safe
并且比 reference
更有说服力。此外 pointers
是狂野的,不应该被太多混淆, pointers to pointers
也是如此 !!!!
指针和引用是新手,所以我不太确定这一点,但我正在尝试传递指针 *minDataValue 和 *maxDataValue,以便在它们从函数返回时更改它们的值。截至目前,在代码中,它们的值没有改变(从测试代码可以看出),我如何设置它们以及我必须更改什么才能使它们通过引用传递以便值可以在函数时更改已经完成了。谢谢!
void findMinAndMax(int array[], int size, int *min, int *max) {
int smallest = array[0];
int largest = array[0];
min = &smallest;
max = &largest;
for (int i = 1; i < size; i++)
{
if (array[i] > largest){
largest = array[i];
}
if (array[i] < smallest){
smallest = array[i];
}
}
// testing code
cout << *min << endl;
cout << *max << endl;
}
int *makeFrequency (int data[], int dSize, int *minDataValue, int *maxDataValue) {
cout << *minDataValue << endl;// testing code
cout << *maxDataValue << endl;// testing code
findMinAndMax(data, dSize, minDataValue, maxDataValue); // How do I pass this so that the value changes after the min and max are found?
cout << *minDataValue << endl; // testing code
cout << *maxDataValue << endl;// testing code
}
int main() {
int dSize;
int *ArrayOfInts;
cout << "How many data values? ";
cin >> dSize;
ArrayOfInts = new int [dSize];
getData(dSize, ArrayOfInts);
int *frequency, min, max;
frequency = makeFrequency(ArrayOfInts, dSize, &min, &max);
}
您可以采用函数采用指向指针的指针:
int *makeFrequency (int data[], int dSize, int **minDataValue, int **maxDataValue)
然后修改
*minDataValue 和 *最大数据值
函数内部。在这种情况下,调用代码必须显式传递一个指向指针的指针:
int *min, *max;
makeFrequency (data, size, &min, &max)
第二种可能是传递对指针的引用:
int *makeFrequency (int data[], int dSize, int*& minDataValue, int *& maxDataValue)
在这种情况下,您只需修改函数内部的指针并将指针传递给函数即可。
我更喜欢第一种方式,因为这样调用代码看起来也不一样,所以指针可能在函数内部被修改的事实也从调用代码中可见。
You have to use pointer of pointer or pass by reference (@Sami Sallinen explained) to change the value *minDataValue and *maxDataValue inside the functions
//Using pointer of pointer
void findMinAndMax(int array[], int size, int **min, int **max)
{
int *smallest = &array[0];
int *largest = &array[0];
min = &smallest;
max = &largest;
for (int i = 1; i < size; i++)
{
if (array[i] > *largest){
*largest = array[i];
}
if (array[i] < *smallest){
*smallest = array[i];
}
}
// testing code
cout << **min << endl;
cout << **max << endl;
}
int *makeFrequency (int data[], int dSize, int *minDataValue, int *maxDataValue)
{
findMinAndMax(data, dSize, &minDataValue, &maxDataValue);
}
有两种实现方式。我会举一个涉及函数和指针的小例子。
void f1 (int** p) // argument is pointer to pointer
{
}
void f2 (int*& p) // argument is reference to pointer
{
}
int main()
{
int x = 5;
int* p = &x;
f1 (&p); // pass address of pointer
f2 (p); // pass reference (C++ type) of pointer
return 0;
}
与 f2
不同,f1
比 C++
更多 C
。但是我喜欢 f2
,因为我觉得它是 type safe
并且比 reference
更有说服力。此外 pointers
是狂野的,不应该被太多混淆, pointers to pointers
也是如此 !!!!