# Thread: Help Using Pointers

1. ## 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. You probably want to pass sum and product to sum_product.

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.

3. 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. 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)`
If you need to keep your prototype as is, you'll have to do your calculations and printing from within sum_product.

5. 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);
}```

6. I agree, this would be much simpler without the pointers but I have to use them

7. In that case you might want to read this http://faq.cprogramming.com/cgi-bin/...&id=1043284351 hope it helps you.

8. ## 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. Been there already.
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.

10. 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

Popular pages Recent additions