问题描述
我想改变,其中三分球最小值和最大值的指针,但似乎变是不是该函数的范围外贴(函数运行后)。运行函数之前,我设定*最小和最大*指向了双M = 0。我是怎样的一个小白所以任何意见将是AP preciated。
INT min_max(双*分,双*最大,诠释大小,加倍[]){
INT I;的printf(\\ N%LG \\ T%LG \\ n,*分,*最大值);分=安培;一个[0];
最大=&放大器;一个[0];对于(i = 1; I<大小;我++){
如果(一个[Ⅰ]≥*最大)
最大=&放大器; A [];
否则如果(一个[1] - *分钟)
分=安培; A [];
}
的printf(\\ N%LG \\ T%LG \\ n,*分,*最大值);
返回0;
}
如果您发送一个指向双,你是允许的双重改变的内容。如果你希望指针改变,你必须发送一个指针的指针(双**
),它允许指针的内容改变。
INT min_max(双**分,双**最大,诠释大小,加倍[]){
INT I; 的printf(\\ N%LG \\ T%LG \\ n,**分,**最大); *分钟=安培;一个[0];
*最大=&放大器;一个[0]; 对于(i = 1; I<大小;我++){
如果(一个[Ⅰ]≥**最大)
*最大=安培; A [];
否则如果(一个[1] - ; **分钟)
*分=安培; A [];
}
的printf(\\ N%LG \\ T%LG \\ n,**分,**最大);
返回0;
}
通过所有间接引用会在这里,它可能是有意义的,以保持两个本地指针甚至只是指数,并在循环的最后一次设定的返回值。
的 的
Well, sort of, but you're asking for a pointer to the array element that is min and max, not the actual value. So even if you passed an array to achieve that, it would have to be an array of pointers (double *minmax[2]
). Now, that's actually just a double**
that points to two double*
values (which you index as element 0 and 1 respectively). So it's the same thing.
Now, why can't you change a pointer? You can! But your changes are confined within the scope of your function. You can't change the value back at the caller because the double*
pointer is passed by value. You might need to do some reading on pass-by-value and pass-by-reference before I go confusing you, but the general idea is this:
Any data type that you send to a function is effectively copied. So if you are passing a double
, that value is copied from the caller into a new memory location (a parameter to the function). The function now has no reference to the original location of that value.
void my_func( double val ) {
val = 42; // Does not affect caller's value because it was copied
}
double value = 1;
my_func( value );
// value is still 1
The same goes when you pass a double*
. You are sending the address of a double
value to the function but the actual address (the pointer) is a value that is copied into the double*
supplied to your function.
void my_func( double* val ) {
*val = 42; // Stuff 42 into the memory location pointed to by 'val'.
}
double value = 1;
my_func( value );
// value is now 42
But your caller appears to want the actual address within the array of the max and min values (unless this was a mistake due to being new to pointers). In that case, a pointer is not enough, because you are copying the contents of your pointers. Here you need to take the address of the memory that holds your pointer and pass that to the function. That address is copied, and when you reference it you are able to write a pointer into that memory location.
void my_func( double** val ) {
*val = *val + 1; // Change the pointer pointed to by 'val'.
}
double values[4] = { 1, 2, 3, 42 };
double *value = &values[2]; // value points to '3'.
my_func( &value );
// value now points to 42
Whenever you're supplying a pointer to a value that you want to be changed by the function, it's referred to as passing by reference.
这篇关于Ç - 传递指针给一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!