Thread: Returning results from a function using pointers

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    43

    Returning results from a function using pointers

    Hello all, I am needing to calculate the amounts of the values stored in the arrays within a function. The calculations within the function however have to be done using pointers. I am having trouble figuring out how to return the value calculated and display it within main. This is what I have so far, any help would be great.
    Code:
    #include <stdio.h>
    double extend (double [], double [], double []);
    
    
    int main ()
    {
        #define max 10
        double price [max] = {10.62, 14.89, 13.21, 16.55, 19.62, 9.47, 6.58, 19.32, 12.15, 3.99};
        double quantity [max] = {4, 9.5,6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.9};
        double amount [max];
        int i;
        
        
            
        for (i=0; i<max; i++){   
             
        printf ("The amount is %f\n", extend(price, quantity, amount));
    }
    
    
    
    
    system ("PAUSE");
    return 0;
    }
    
    
    double extend (double price[], double quantity[], double amount [])
    {
        int i;
        double *gPtr, *mPtr, *nPtr;
        
        gPtr= price;
        mPtr= quantity;
        nPtr= amount;
        
        
        
        for (i=0; i<max; i++){
            *nPtr++= *mPtr++ * *gPtr++;
            }
            
    return (amount);
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You want to return a pointer to an array but you have declared extend() to return a double. That doesn't work.

    Your results are stored in "amount" after calling the function, thus the easiest solution is to change the return value to void, call the function once in main and then iterate through the array and print the values.

    Bye, Andreas

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Hmm.. If i were you i wouldn't return anything..Why?

    Because the arrays are really passed by pointer...So what changes are going to be made to them are going to be "alive", even after the function terminates.

    Now because you assign the pointers correct to the addresses of the pointers, these local pointers inside your function are going to affect the memory where price,quantity and amount are pointing to..Thus the modifications made inside the function will be preserved.

    So modify the function extend to have as return value a void instead of a double.Do not forget to modify the prototype of the function too
    Of course now because the return value is void, the return amount (line 43) should be removed.
    Also something must be modified in the main function too.Think that and post back if needed

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Thanks all that worked great I changed the function to a void, declared the function in main and then changed the print function a bit and I got this:
    Code:
    #include <stdio.h>
    void extend (double [], double [], double []);
    
    
    int main ()
    {
        #define max 10
        double price [max] = {10.62, 14.89, 13.21, 16.55, 19.62, 9.47, 6.58, 19.32, 12.15, 3.99};
        double quantity [max] = {4, 9.5,6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.9};
        double amount [max];
        int i;
        
        extend (price, quantity, amount);
            
        for (i=0; i<max; i++){   
             
        printf ("The amount is %f\n", amount[i]);
    }
    
    
    
    
    system ("PAUSE");
    return 0;
    }
    
    
    void extend (double price[], double quantity[], double amount [])
    {
        int i;
        double *gPtr, *mPtr, *nPtr;
        
        gPtr= price;
        mPtr= quantity;
        nPtr= amount;
        
        
        
        for (i=0; i<max; i++){
            *nPtr++= *mPtr++ * *gPtr++;
            }
    }
    Thanks a ton!!!

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are welcome

    Also a tip.When we use define, we place it before main, so its global.But for some reason you may need it to be not global ( i do not really know what that reason can be).
    But always , it is good to write the defined word with all caps, like this
    Code:
    #define MAX 100
    an all-caps variable immediately states to user of your code, that this variable he is looking to is a constant one!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function returning pointers
    By capricorn in forum C Programming
    Replies: 5
    Last Post: 12-16-2010, 08:04 AM
  2. Not returning correct results
    By drty2 in forum C Programming
    Replies: 4
    Last Post: 01-19-2009, 12:39 PM
  3. Help with struct... not returning correct results.
    By drty2 in forum C Programming
    Replies: 7
    Last Post: 01-18-2009, 11:25 PM
  4. Returning address of Function. Pointers to Functions.
    By edunia11 in forum C Programming
    Replies: 2
    Last Post: 12-04-2006, 12:28 PM
  5. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 11:37 PM

Tags for this Thread