1. ## Pointers

Hi there, I'm trying to create a basic program that will test to see if a user's input number is the sum of two cubes. What I have so far is:

Code:
```//Tests if number is sum of two cubes
//Date: 19/07/12

#include <stdio.h>

int cubesTest(int x, int *A, int *B);

int main(int argc, char *argv[]) {

int input=0;
int cubeA=-1;
int cubeB=-1;
int value=-1;

printf("\t\tInput number: ");
scanf("%d", &input);
value = cubesTest(input, &cubeA, &cubeB);
printf("\t\t");

switch(value) {
case 0:
printf("%d is not a sum of 2 cubes\n", input);
break;
case 1:
printf("%d = %d^3 + %d^3\n", input, cubeA, cubeB);
break;
}

return 0;
}

int cubesTest(int x, int *cubeA, int *cubeB) {

int cubeA=1;
int cubeB=1;
int flag=0;

for(cubeA=1; cubeA*cubeA*cubeA < x; cubeA++) {
for(cubeB=1; cubeB*cubeB*cubeB < x; cubeB++) {
if (cubeA*cubeA*cubeA + cubeB*cubeB*cubeB == x) {
flag = 1;
*cubeA = cubeA;
*cubeB = cubeB;
}
}
}

return flag;
}```
And while my code can successfully test to see if a number is the sum of two cubes, what fails is the last print statement in the main function:

Code:
`printf("%d = %d^3 + %d^3\n", input, cubeA, cubeB);`
because I'm confused with how the pointers are supposed to work. I'm not sure with how to pull the value of cubeA and cubeB (if the input number is found to be a sum of 2 cubes) from the cubesTest function.

Also, it doesn't compile because of the misuse of pointers. Any tips and help would be much appreciated, thankyou.

2. You will find things less confusing if you the variables local to your function had different names from the arguments.

3. I took your advice, and now it works wonderfully! I changed my function into

Code:
```int cubesTest(int x, int *cubeA, int *cubeB) {

int n=1;
int m=1;
int flag=0;

for(m=1; m*m*m < x; m++) {
for(n=1; n*n*n < x; n++) {
if (m*m*m + n*n*n == x) {
flag = 1;
*cubeA = m;
*cubeB = n;
}
}
}

return flag;
}```
I'm a little confused as to why I couldn't cubeA and cubeB as variables again though.

4. Originally Posted by Mentallic
I'm a little confused as to why I couldn't cubeA and cubeB as variables again though.
For the same reason as there is only one "Mentallic" on this forum. How could we distinguish you from another "Mentallic"?

Bye, Andreas

5. Originally Posted by AndiPersti
For the same reason as there is only one "Mentallic" on this forum. How could we distinguish you from another "Mentallic"?

Bye, Andreas
Oh sorry, I had the impression that n and *n were separate variables in a sense, but I remember now that all the examples I've seen so far have had *np to denote the pointer for the variable n.