Pointer problem I think

This is a discussion on Pointer problem I think within the C Programming forums, part of the General Programming Boards category; Hey guys, I am having a problem using pointers. In function process_current, I call the function assign_vals, at the end ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    3

    Pointer problem I think

    Hey guys, I am having a problem using pointers. In function process_current, I call the function assign_vals, at the end of assign vals, we have values for v1 and v2, but when printed out the values in process_current, we're back at zero?

    Please help, here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    const char VOLTAGE[] = "voltage";
    const char CURRENT[] = "current";
    const char RESISTANCE[] = "resistance";
    
    void process_voltage(double current, double resist);
    void process_resistance(double current, double voltage);
    void process_current(double voltage, double resist);
    void assign_vals(const char s1[], double *v1, const char s2[],
                     double *v2);
    void display_result(const char var1[], double *v1, const char var2[],
                        double *v2, const char var3[],double *v3);
    
    int main()
    {
            double voltage = 0;
            double resist = 0;
            double current = 0;
            char unknownval[12];
            int read;
    
            printf("What do you wish to know:  ");
            read = scanf("%s", unknownval);
    
            while(read == 1)
            {
               if(!strcmp (unknownval, VOLTAGE))
               {
                  process_voltage(resist, current);
               }
    
               else if(!strcmp (unknownval, RESISTANCE))
               {
                  process_resistance(voltage, current);
               }
    
               else if(!strcmp (unknownval, CURRENT))
               {
    {
            double voltage = 0;
            double resist = 0;
            double current = 0;
            char unknownval[12];
            int read;
    
            printf("What do you wish to know:  ");
            read = scanf("%s", unknownval);
    
            while(read == 1)
            {
               if(!strcmp (unknownval, VOLTAGE))
               {
                  process_voltage(resist, current);
               }
    
               else if(!strcmp (unknownval, RESISTANCE))
               {
                  process_resistance(voltage, current);
               }
    
               else if(!strcmp (unknownval, CURRENT))
               {
                  process_current(voltage, resist);
               }
    
               else
               {
                  printf("Incorrect unknown value %s. ", unknownval);
                  printf(" Exiting.\n");
                  exit(1);
               }
    
               read = scanf("%s", unknownval);
            }
             
            printf("\n");
                
            return 0;
    }
    
    void process_voltage(double resist, double current) 
    {
            double voltage;
                
            assign_vals(RESISTANCE, &resist, CURRENT, &current);
    
            voltage = resist * current;
               }
            
               else
               {
                  printf("Incorrect unknown value %s. ", unknownval);
                  printf(" Exiting.\n");
                  exit(1);
               }
            
               read = scanf("%s", unknownval);
            }
             
            printf("\n");
                
            return 0;
    }
    
    void process_voltage(double resist, double current) 
    {
            double voltage;
                
            assign_vals(RESISTANCE, &resist, CURRENT, &current);
    
            voltage = resist * current;
                  
            display_result(RESISTANCE, &resist, CURRENT, &current,
                           VOLTAGE, &voltage);
    }
                
    void process_resistance(double current, double voltage)
    {
            double resist;
                
            assign_vals(VOLTAGE, &voltage, CURRENT, &current);
    
            resist = voltage / current;
             
            display_result(VOLTAGE, &voltage, CURRENT, &current,
                           RESISTANCE, &resist);
    }
     
    void process_current(double voltage, double resist)
    {
            double current;
    
            assign_vals(VOLTAGE, &voltage, RESISTANCE, &resist);
    
        //Values in voltage and resist are zero, not what they were in assign_vals
            printf("%lf", voltage);
            printf("%lf", resist);
            current = voltage / resist;
    
            display_result(VOLTAGE, &voltage, RESISTANCE, &resist,
                           CURRENT, &current);
    }
             
    void assign_vals(const char var1[], double *v1, const char var2[],
                     double *v2)
    {
            double value1;
            double value2;
            char str1[12];
            char str2[12];  
    
            printf("First variable:  ");
            scanf("%s", str1);
            printf("Value:  ");
            scanf("%lf", &value1);
            printf("Second variable:  ");
            scanf("%s", str2);
            printf("Value:  ");
            scanf("%lf", &value2);
     
            if(!strcmp(str1, str2))
            {
               printf("not enough unique variables. exiting\n");
               exit(1);
            }
                
            else if((!strcmp(str1, var1) && !strcmp(str2, var2)))
            {
               v1 = &value1;
               v2 = &value2;
            }
    
            else if((!strcmp(str1, var2) && !strcmp(str2, var1)))
            {
               v1 = &value2;
               v2 = &value1;
            }
     
            else
            {
               printf("variables are not valid for this option.");
               printf("  exiting\n");
               exit(1);
            }
    
       //Prints out the values in v1 and v2 correctly, now go back to process_current   //function
            printf("%lf", *v1);
            printf("%lf", *v2);
    }
    
    void display_result(const char str1[], double *v1, const char str2[],
                        double *v2, const char str3[], double *v3)
    {
            printf("\n");
            printf("%s = %.2lf", str1, *v1);
            printf("%s = %.2lf", str2, *v2);
            printf("%s = %.2lf", str3, *v3);
    }
    Let me know if I am not making sense. Thanks for the time.

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    The following, in assign_vals():
    Code:
    v1 = &value2;
    sets the local variable (parameters are local to a function) v1 to have the value &value2. What you want is:
    Code:
    *v1 = value2;
    This says to store value2 in the location that v1 points to.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    3
    Quote Originally Posted by cas View Post
    The following, in assign_vals():
    Code:
    v1 = &value2;
    sets the local variable (parameters are local to a function) v1 to have the value &value2. What you want is:
    Code:
    *v1 = value2;
    This says to store value2 in the location that v1 points to.
    IT WORKS! Thank you so much and thanks for the reasoning behind it. Greatly appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with pointer to a pointer variable
    By Rodri in forum C Programming
    Replies: 2
    Last Post: 11-20-2011, 09:50 AM
  2. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  4. Replies: 4
    Last Post: 11-05-2006, 01:57 PM
  5. pointer to pointer how do i solve following problem?
    By kobra_swe in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21