Thread: Pointers no Pointing

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    13

    Pointers no Pointing

    Hey, still new to C and struggling in basic syntax, pointers in functions arguments keep turning up errors.
    Code:
    void target(&x_target_pos, &y_target_pos)
    Error: ||=== Build: Debug in Graphics1 (compiler: GNU GCC Compiler) ===|
    /home/user/Programming/Expo_1_Temp/src/main.c|55|error: expected declaration specifiers or ‘...’ before ‘&’ token|
    /home/user/Programming/Expo_1_Temp/src/main.c|55|error: expected declaration specifiers or ‘...’ before ‘&’ token|

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    If you're trying to declare a new function, this isn't the way to do it. Arguments need types too, and "*" instead of "&" if you want them to be pointers.

    If you're trying to call an existing function, this isn't the way to do it either. Remove the "void".
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    13
    No, I have two functions, one gives random coordinates one uses those random coordinates to place a target in a graphics window. But I cant keep the random coordinates as a constant variable.

  4. #4
    Registered User
    Join Date
    Apr 2017
    Posts
    13
    The problem is I have one function that should generate random coordinates and one that uses the coordinates to place a target. The reason I had to do it this way is because I have two sprites for my stickman and when he throws the target the sprites switch, to do that i have to clear the screen which means i have to get rid of the target as well. So The random coordinates have to be constant so i can replace the target at the same location that it was before the screen cleared. My syntax is just off somewhere. If there is a simpler solution I can't really see it.
    Now that I've changed them to int* instead of "&" I dont get errors but that target is not placed, I think this is because the pointers hold no values? Or maybe I've made another mistake in implementation.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    If your code isn't too big (more than 500 lines or so), why don't you post it here, in order for us to see what might be wrong. Remember to copy as plain text and put it in code tags.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Apr 2017
    Posts
    13

    my code

    GFX are graphics function
    Code:
    #include <graphics_lib.h>
    #include <math.h>
    #include <time.h>
    #include <stdio.h>
    
    /* globals */
    /*draw stickman*/
    void stickman(int user_input, int user_input_2)
    {
        //head
        GFX_DrawCircle(user_input, user_input_2, 20, 2);
        //body
        GFX_DrawLine(user_input, user_input_2+20,user_input,user_input_2+70, 2);
        //arm
        GFX_DrawLine(user_input+0, user_input_2 +30,user_input-50,user_input_2+30, 2);
        //left arm holding coin
        GFX_DrawLine(user_input-50,user_input_2+30,user_input-65, user_input_2+10,2);
        //right arm holding coin
        GFX_DrawLine(user_input+0,user_input_2+30,user_input-55,user_input_2+10,2);
        //draw coin (projectile)
        GFX_DrawCircle(user_input-60,user_input_2-10,25,5);
        GFX_DrawCircle(user_input-60,user_input_2-10,15,3);
        //left leg
        GFX_DrawLine(user_input, user_input_2+70,user_input-80,user_input_2+120, 2);
        //right leg
        GFX_DrawLine(user_input, user_input_2+70,user_input+80,user_input_2+120, 2);
    
        GFX_UpdateDisplay();
    }
    /*draw stickman without coin for after projectile thrown*/
    void stickman_coinless(int user_input, int user_input_2)
    {
        //head
        GFX_DrawCircle(user_input, user_input_2, 20, 2);
        //body
        GFX_DrawLine(user_input, user_input_2+20,user_input,user_input_2+70, 2);
        //arm
        GFX_DrawLine(user_input, user_input_2 +30,user_input+50,user_input_2+30, 2);
        //arm
        GFX_DrawLine(user_input, user_input_2 +30,user_input-60,user_input_2-10, 2);
        //left leg
        GFX_DrawLine(user_input, user_input_2+70,user_input-80,user_input_2+120, 2);
        //right leg
        GFX_DrawLine(user_input, user_input_2+70,user_input+80,user_input_2+120, 2);
    
        GFX_UpdateDisplay();
    }
    int target_placement(int* x_target_pos, int* y_target_pos)
    {
      //make random coordinates for target to appear at
        *x_target_pos = GFX_RandNumber(200, 600);
        *y_target_pos = GFX_RandNumber(1, 460);
        return 0;
    }
    void target(int* x_target_pos, int* y_target_pos)
    {
        //draw target
        GFX_DrawCircle(x_target_pos, y_target_pos, 40, 4);
        GFX_DrawCircle(x_target_pos, y_target_pos, 30, 1);
        GFX_DrawCircle(x_target_pos, y_target_pos, 20, 4);
        GFX_DrawCircle(x_target_pos, y_target_pos, 5, 1);
        GFX_UpdateDisplay();
    }
    
    //draw projectile (coin)
    int projectile(int user_input,int user_input_2,float user_input_3,float user_input_4)
    {
        //set inital pos of coin
        float pos_x=user_input-60;
        float pos_y=user_input_2-10;
        //PI to a value
        int pie = 3.142;
        //move to inital
        GFX_MoveTo(pos_x, pos_y);
        //set variables
        int inital_y=pos_y;
        int inital_x=pos_x;
        float time=1;
        int gravity=9.81;
        double horiz = user_input_3*cos(user_input_4 *pie/180);
        double vertical = user_input_3*sin(user_input_4 *pie/180);
        //caculate trajectory
        while(pos_y<480)
        {
            time=(pos_x-inital_x)/horiz;
            pos_y=(inital_y)-(vertical*time)+(gravity*time*time)/2;
            GFX_DrawLineTo(pos_x,pos_y,3);
    
            pos_x+=1;
    
            printf("%f %f %f\n", pos_x, pos_y, time);
    
        }
        GFX_UpdateDisplay();
    }
    
    int main(void)
    {
        GFX_InitWindow(640, 480);
        //white for drawing
        GFX_SetColour(RED);
        //green to symbolise start
        GFX_SetBackgroundColour(GREEN);
        GFX_UpdateDisplay();
        GFX_ClearWindow();
        //define varibale ans for user choice of carrying on
        int x_target_pos;
        int y_target_pos;
        char ans;
    
            //loop to ask continue
            do
            {
                printf("\nWould you like to play(Y/N)?: \n");
                scanf("%c,", &ans);
                getchar();
                GFX_SetBackgroundColour(GREEN);
                GFX_ClearWindow();
                target_placement(&x_target_pos, &y_target_pos);
                target(&x_target_pos, &y_target_pos);
                GFX_UpdateDisplay();
    
                    //for man position
                     int user_input;
                    printf("Please enter coordinate x: ");
                    scanf("%d,", &user_input);
                    getchar();
    
                    int user_input_2;
                    printf("Please enter coordinate y: ");
                    scanf("%d,", &user_input_2);
                    getchar();
    
                    //put coin thrower down
                    stickman(user_input,user_input_2);
                    GFX_UpdateDisplay();
    
                    float user_input_3;
                    printf("Please specify velocity of projectile: ");
                    scanf("%f,",&user_input_3);
                    getchar();
    
                    float user_input_4;
                    printf("Please enter the angle: ");
                    scanf("%f,",&user_input_4);
                    getchar();
    
                    //clear window and place in coinless stickman
                    GFX_ClearWindow();
                    stickman_coinless(user_input,user_input_2);
                    target(&x_target_pos, &y_target_pos);
                    projectile(user_input,user_input_2,user_input_3,user_input_4);
                    GFX_SetBackgroundColour(RED);
                    GFX_UpdateDisplay();
                    GFX_ClearWindow();
                    //ask to redo or end
                    printf("\nWould you like to go again(Y/N)?: \n");
                    scanf("%c",&ans);
                    getchar();
    
                    GFX_UpdateDisplay();
                    } while (ans == 'y' || ans == 'Y');
    
                        /* Wait for a user's signal to exit*/
                        printf("Please press enter");
                        getchar();
                        getchar();
    
                        /* remove the display */
                        GFX_CloseWindow();
    
                        return 0;
    
        }

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    In target function I found that you are passing pointers instead of value. You have to dereference the pointers to the graphics functions.

  8. #8
    Registered User
    Join Date
    Apr 2017
    Posts
    13
    what do you mean? could you give me an example?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > scanf("%c,", &ans);
    > getchar();
    It would be a lot easier to just remove the trailing comma in for scanf format strings (all of them).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2017
    Posts
    13
    oh yeah, but that isnt causing my current problem

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Why did you do this?
    void target(int* x_target_pos, int* y_target_pos)
    when target doesn't need to update the input parameters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Apr 2017
    Posts
    13
    made that change, now before the do loop x_target_pos=4207520 and y_target_pos=0. But after respectively 219, 47, the target still doesnt show in the graphics window though.

  13. #13
    Registered User
    Join Date
    Apr 2017
    Posts
    13
    fyi they are supposed to be x_target_pos in range 200-400 and y_target_pos in range 1-460

  14. #14
    Registered User
    Join Date
    Apr 2017
    Posts
    13
    Nope nvm, when calling the functions I needed to get rid of the "&"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 06-19-2015, 02:30 PM
  2. Manipulate the pointers pointing to address
    By redsmolf in forum C Programming
    Replies: 7
    Last Post: 04-23-2015, 02:19 PM
  3. array of pointers pointing to array of obj
    By thestien in forum C++ Programming
    Replies: 10
    Last Post: 10-14-2006, 10:41 AM
  4. Pointing to a Queue of Pointers?
    By Dragoon_42 in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2004, 09:19 PM
  5. array of pointers each pointing to an array
    By muttski in forum C Programming
    Replies: 6
    Last Post: 05-01-2002, 02:03 PM

Tags for this Thread