Pointers

This is a discussion on Pointers within the C Programming forums, part of the General Programming Boards category; Hi there, I'm trying to create a basic program that will test to see if a user's input number is ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    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. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    You will find things less confusing if you the variables local to your function had different names from the arguments.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    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. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Mentallic View Post
    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. #5
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Quote Originally Posted by AndiPersti View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  4. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM
  5. Assigning pointers from pointers in struct.
    By Brian in forum C Programming
    Replies: 2
    Last Post: 10-18-2003, 04:30 AM

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