Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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("\n%lg\t%lg\n", *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("\n%lg\t%lg\n", *min, *max);                                                                                                   
 return 0;                                                                                                                             
}
like image 372
csta Avatar asked Dec 06 '22 12:12

csta


1 Answers

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("\n%lg\t%lg\n", **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("\n%lg\t%lg\n", **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.

like image 155
paddy Avatar answered Dec 14 '22 07:12

paddy