Thread: Help Using Pointers

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    5

    Help Using Pointers

    I have listed my code. My numbers are being read but the product and sum are not displaying the correct answer. What am I missing? Any suggestions?

    Code:
    #include <stdio.h>
    
    void sum_product(int* x, int* y);
    
    void main()
    {
    	int num1;
    	int num2;
    	double product;
    	int sum;
    	
    	
    
    
    	printf("Enter two integers: ");
    	scanf("%d%d", &num1, &num2);
    	
    	printf("The # is %d and %d\n", num1, num2);
    	
    	
    	sum_product(&num1, &num2);
    
    	printf("The sum is %d\n", &sum);
    	printf("The product is %d\n", &product);
    
    
    }
    
    
    void sum_product(int *x, int *y)
    {		
    
    	double *product =  (*x) * (*y);
    	int *sum = *x+*y;
    	
    	
    
    	
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You probably want to pass sum and product to sum_product.

    [edit]Don't use void main -- see the FAQ if you are tempted to ask why.
    Code:
    	printf("The sum is %d\n", &sum);
    	printf("The product is %g\n", &product);
    Remove the &'s, and use a floating-point format specifier for product.
    Last edited by Dave_Sinkula; 03-17-2005 at 04:23 PM. Reason: Little tweaks.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    I do not understand what you mean. I am new to using pointers. In this program I have to find the sum and product of two integers inputed by the user using the function prototype: void sum_product(int *x, int *y); I do not understand what I am doing wrong.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    void sum_product(int *x, int *y)
    {		
    	double *product =  (*x) * (*y);
    	int *sum = *x+*y;
    }
    Here product and sum are pointers. They do not point to any valid memory that you can write to. Yet you go ahead and calculate a value and trample right over the memory that you can't write to by writing to it.

    I would expect the function to be defined like this.
    Code:
    void sum_product(int *x, int *y, double *product, int *sum)
    [edit]If you need to keep your prototype as is, you'll have to do your calculations and printing from within sum_product.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Do you have to use pointers for this problem or you can do it any other way? I really dont see why you would want to use pointers for such a simple problem. You can have 2 functions instead, 1 for the sum and 1 for the product to make your job easier and the program more readable. Here is a tip for you
    Code:
    #include <stdio.h>
    
    int product(int num1, int num2);
    
    int main()
    {
    	int num1;
    	int num2;
    
    
    	printf("Enter two integers: ");
    	scanf("%d%d", &num1, &num2);
    	
    	printf("The # is %d and %d\n", num1, num2);
    	printf("The product is %d\n",product(num1,num2));
    
    }
    
    
    int product(int num1, int num2)
    {		
    	return (num1 * num2);	
    }
    When no one helps you out. Call google();

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    I agree, this would be much simpler without the pointers but I have to use them

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    In that case you might want to read this http://faq.cprogramming.com/cgi-bin/...&id=1043284351 hope it helps you.
    When no one helps you out. Call google();

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    5

    Pointers

    I changed my code based on the prototype suggested. but results are still not right. My results are displaying sum = 1245036 and product = 1245040

    Code:

    Code:
    #include <stdio.h>
    
    void sum_product(int *x, int *y, double *product, int *sum)
    {		
    
    	*product =  (*x) * (*y);
    	*sum = *x+*y;
    }
    
    
    main()
    {
    	int num1;
    	int num2;
    	double product;
    	int sum;
    	
    	printf("Enter two integers: ");
    	scanf("%d%d", &num1, &num2);
    	
    	printf("The # are %d and %d\n", num1, num2);
    	
    	
    	sum_product(&num1, &num2, &product, &sum);
    
    	printf("The sum is %d\n", &sum);
    	printf("The product is %d\n", &product);
    
    
    }

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Been there already.
    Quote Originally Posted by Dave_Sinkula
    Code:
    	printf("The sum is %d\n", &sum);
    	printf("The product is %g\n", &product);
    Remove the &'s, and use a floating-point format specifier for product.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Thank you so much! Everything is working fine. Can you please tell me where I can find more information on the %g. I have never used that before

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Last edited by Dave_Sinkula; 03-17-2005 at 05:00 PM. Reason: How silly of me to forget!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

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. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  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