Thread: Help with returning arrays using pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    11

    Help with returning arrays using pointers

    I'm writing a program that needs to have several functions. Other than main, one of them is a function called get_inputs. In get_inputs, the program will get information from the user, and then return all 4 of the values recieved to main.

    I know that pointers need to be used here, so here is what I think needs to be done, but I know that it wont work.

    First of all, I need to properly call the function in main. I also need to find out if the way that I've implemented the pointers is correct. I'm not sure what I need to put inside the braces in the userInput array. I don't even know if the format is correct. I've done some research, but it is really confusing for me. Could I get some help with both of these?

    NOTE: there are more functions in the program, but they dont apply to this.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    /* Rows represent the months of the year, Columns represent the hours in a typical day */
    #define NROWS 11
    #define NCOLS 23
    #define TRUE 1
    #define FALSE 0
    #define INSTALLATION_COST 2000
    
    float panelEfficiency = 0.18, inverterEfficiency = 0.90;
    
    int main()
    {
          int houseEnergyREQ, numBatteries, numInverters;
          int flag = TRUE;
          float userInput[4], panelCost, inverterCost, batteryCost,
                panelArea, batteryRating = 4.8, inverterRating = 5.0,
                insolDayMin, maxHourPanelOutput, insolHourMax, totalCost;
          
          float *ptr_to_userInput;
          
          float insolation_Matrix[NROWS][NCOLS];
    
          
          while(flag)
          {
                float get_inputs(float userInput[]);
                
                /* Switch userInput array to meaningful terms */
                userInput[0] = houseEnergyREQ;
                userInput[1] = panelCost;
                userInput[2] = inverterCost;
                userInput[3] = batteryCost;
                
                /* Equation to calculate panelArea */
                panelArea = houseEnergyREQ/(insolDayMin*panelEfficiency*inverterEfficiency);
                
                /* Equation to calculate maxHourPanelOutput */
                maxHourPanelOutput = insolHourMax*panelArea*panelEfficiency;
                
                /* Equation to calculate numbatteries */
                numBatteries = ceil(houseEnergyREQ/(inverterEfficiency*batteryRating));
                
                /* Equation to calculate numInverters */
                numInverters = ceil(maxHourPanelOutput/(inverterEfficiency*inverterRating));
                
                /* Equation to calculate total cost */
                totalCost = panelArea*panelCost + numBatteries*batteryCost + numInverters*inverterCost
                             + INSTALLATION_COST;
                
                printf("The total cost of your Solar panel system is %.2f $. \n", totalCost);
                
                /* Give user an option to quit program */
                printf("Do you wish to continue? (y/n) \n");            
                if(getchar() == 'n')
                {
                      flag = FALSE;
                }
          }
    
          system("PAUSE");
    }
    /*---------------------------------------*/
    /* Function to get inputs houseEnergyREQ, panelCost, inverterCost, batteryCost */
    float get_inputs(float userInput[4], *ptr_to_userInput)
    {
          *ptr_to_userInput = &userInput[]
          
          printf("This program will calculate the total cost of your solar panel system.");
          printf("Enter the daily required amount of energy for your house, in kWh : \n");
          scanf("%f", &userInput[0]);
    
          printf("Enter the cost of solar panels (per m^2), power inverters and batteries: \n");
          scanf("*f %f %f", &userInput[1], &userInput[2], &userInput[3]);
    
          return *ptr_to_userInput;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since arrays are always passed as "the address of the first element" in C, you don't actually need to add another argument. Just use the array as input.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    How would I do that?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void get_inputs(float userInput[4])
    {
          printf("This program will calculate the total cost of your solar panel system.");
          printf("Enter the daily required amount of energy for your house, in kWh : \n");
          scanf("%f", &userInput[0]);
    
          printf("Enter the cost of solar panels (per m^2), power inverters and batteries: \n");
          scanf("*f %f %f", &userInput[1], &userInput[2], &userInput[3]);
    }
    Like that - now whatever array you pass in will be modified by the input from the user.

    Obviously, you have no checking to see if the user entered "a" as the first input and all the input is "garbage". You should check that the return value from scanf is equal to the number or input values you expect (that is 1 and 3 respectively), and if not, you should clear the input and try again. But if you don't care about error handling, just leave it as above.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    Oh, so if I change the function from float to void, I don't need a return statement?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cuba06 View Post
    Oh, so if I change the function from float to void, I don't need a return statement?
    Correct. And since the array is passed as a pointer in the first place, you can change it inside the function. And of course, you can't return multiple values anyways, so it's no loss to remove the return statement.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing and returning array pointers?
    By thealmightyone in forum C Programming
    Replies: 26
    Last Post: 03-26-2009, 03:38 PM
  2. Pointers to 2D arrays
    By bertazoid in forum C Programming
    Replies: 16
    Last Post: 02-26-2009, 04:46 PM
  3. Functions returning char arrays
    By turmoil in forum C Programming
    Replies: 3
    Last Post: 05-27-2003, 01:43 AM
  4. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM
  5. Hello all and pointers to multi-dimensional arrays
    By Mario in forum C++ Programming
    Replies: 16
    Last Post: 05-17-2002, 08:05 AM