Thread: calculations with pointers

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    calculations with pointers

    hey all, so im making one that uses pointers to pass the answers of four calculations. im just missing something here, i go to compile it and it freezes and closes the program. thanks for the help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void calculation(double, double, double *, double *, double *, float *);
    
    int main()
    {
        double num1, num2;
        
        double *sum, *sub, *prod;
        float *div;
        
        float div_answer = 0;
        double sum_answer = 0;
        double sub_answer = 0;
        double prod_answer = 0;
        
                         
                          *sum = sum_answer;
                          *sub = sub_answer;
                          *div = div_answer;
                          *prod = prod_answer;         
                          
        printf("Please enter two numbers:");
        scanf("%f %f",&num1, &num2);
        
        calculation (num1, num2, &sum_answer, &sub_answer, &prod_answer, &div_answer);
        
                          printf("%d + %d = %d\n", num1, num2, sum_answer);
                          printf("%d - %d = %d\n", num1, num2, sub_answer);                      
                          printf("%d * %d = %d\n", num1, num2, prod_answer);    
                          printf("%d / %d = %f\n", num1, num2, div_answer);
        
        system("PAUSE");
      return 0;
    }
    
     void calculation (double num1, double num2, double *sum, double *sub, double *prod, float *div)
          {           
                      
                      *sum = num1 + num2;
                      
                      *sub = num1 - num2;
                     
                      *prod = num1 * num2;
                  
                      *div = num1 / num2;
                     
                      }

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mackieinva View Post
    Code:
                          *sum = sum_answer;
                          *sub = sub_answer;
                          *div = div_answer;
                          *prod = prod_answer;
    This is the problem. You are assigning values to random spots in memory, since the four pointers on the left-hand-side are all uninitialized. This entire block of code, as well as the sum, sub, div, and prod pointers, are all unnecessary. Just take it out.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    ty for the help brewbuck, it helped make it click. i wasnt trying to be an ass myself calling you a dick before just hard to tell sarcasm and things like that over the internet. later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM