Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8489817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:56:00+00:00 2026-06-10T21:56:00+00:00

I’m trying to change where the pointers min and max are pointer to, but

  • 0

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;                                                                                                                             
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:56:02+00:00Added an answer on June 10, 2026 at 9:56 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.