Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

c - passing pointers to a function

I'm trying to change where the pointers min and max are pointer to, but it seems that the "change" is not sticking outside the function's scope (after the function has run). Before running the function I set *min and *max to point to a "double m = 0". I'm kind of a NOOB so any advice would be appreciated.

int min_max(double * min , double * max , int size , double a[]){                                                                       
int i;                                                                                                                                

printf("
%lg%lg
", *min, *max);                                                                                                   

min = &a[0];                                                                                                                          
max = &a[0];                                                                                                                          

for (i = 1 ; i < size ; i++) {                                                                                                        
 if (a[i] > *max)                                                                                                                    
  max = &a[i];                                                                                                                      
 else if (a[i] < *min)                                                                                                               
  min = &a[i];                                                                                                                      
 }                                                                                                                                     
 printf("
%lg%lg
", *min, *max);                                                                                                   
 return 0;                                                                                                                             
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you send a pointer to a double, you are allowing the contents of that double to change. If you want the pointer to change, you must send a pointer to the pointer (double**), which allows the contents of the pointer to change.

int min_max(double ** min , double ** max , int size , double a[]){  
    int i;                                                                                                                                

    printf("
%lg%lg
", **min, **max);                                                                                                   

    *min = &a[0];                                                                                                                          
    *max = &a[0];                                                                                                                          

    for (i = 1 ; i < size ; i++) {                                                                                                        
        if (a[i] > **max)                                                                                                                    
            *max = &a[i];                                                                                                                      
        else if (a[i] < **min)                                                                                                               
            *min = &a[i];                                                                                                                      
    }                                                                                                                                     
    printf("
%lg%lg
", **min, **max);                                                                                                   
    return 0;                                                                                                                             
}

With all the dereferencing going on here, it may make sense to keep two local pointers or even just the indices, and set the return values once at the end of the loop.

[edit]

Why is it that I can't change a pointer in a function? If I instead send an array (a pointer like thing) to retain the min and max values, say, minmax[0]=min, and minmax[1]=max, the values will change, no?

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...