Problem with Passing arguments/pointers..PLEASE HELP

This is a discussion on Problem with Passing arguments/pointers..PLEASE HELP within the C Programming forums, part of the General Programming Boards category; Ive been trying to debug this peice of code for a couple days now, no matter what I try I ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    17

    Exclamation Problem with Passing arguments/pointers..PLEASE HELP

    Ive been trying to debug this peice of code for a couple days now, no matter what I try I continue to have the same problem..im trying to pass several arguments of different types from one function to a function..but It appears as if some how the parameters are being changed from one function to another, or they're not referencing properly the initial calling function is
    Code:
    bool draw_gemstones(struct PacmanBoard* pcmb_p){
    
    
    
    	struct Gemstone *gemstn_p=(struct Gemstone*)malloc(sizeof(Gemstone));
    	
    	construct_gemstone(gemstn_p);
    	
    	init_gemstones(gemstn_p,pcmb_p,0.5,1,0);
    
    	draw_Polygon(gemstn_p->gemstone);
    
    	destruct_Polygon(gemstn_p->gemstone);
    
    
    return true;
    }
    the problem is, when i look at x and y init_gemstone() (below) the values change from 1,0 to something like 12942342,12942342 (very large and x=y)

    Code:
    bool init_gemstones(struct Gemstone* gem_p,struct PacmanBoard* pcmb_p,double size,int x,int y){
    
    
    
    	//get dimensions of individual cell
    	double cell_w = pcmb_p->width / (double)(pcmb_p->ncells_x);
    	double cell_h = pcmb_p->height / (double)(pcmb_p->ncells_y);
    
    	struct Point* points = (struct Point*)malloc(4*sizeof(Point));
    
    	//find center position of cell
    	gem_p->position->x=-1+(x*cell_w)+(cell_w/2); //BREAK POINT
    	gem_p->position->y=1-(y*cell_h)+(cell_h/2);
    
    	points[0].x=gem_p->position->x-cell_w*(size/2);
    	points[0].y=gem_p->position->y;					
    	
    	points[1].x=gem_p->position->x;
    	points[1].y=gem_p->position->y+cell_h*(size/2);
    
    	points[2].x=points[0].x+size*cell_w;
    	points[2].y=points[0].y;
    
    	points[3].x=points[1].x;
    	points[3].y=points[1].y-size*cell_h;
    
    
    	init_Polygon(gem_p->gemstone, gem_p->position, 4, points, 1.0, 1.0, 1.0);
    
    
    
    return true;
    }
    when i have been trying to debug i place a break point at the line
    gem_p->position->x=-1..., i tried to place a break point at the line below it, but it says the break point will never be hit?

    PLEASE HELP..I normally would ask people to help debug my work, but im desperate, ive been staring at this for days now with out accomplishing anything...

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    What does "gem_p->position" look like? Stucture wise... You may be overflowing the values, depending on the values of x and y you pass to init_gemstone().

    What exactly is your problem? You haven't made it all that clear.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you throw a bunch of printf statements in there showing the value of each item before you start assigning?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    heres one of my header files with the stuct for gem_p
    Code:
    #ifndef _UOW_GEMSTONE_H
    #define _UOW_GEMSTONE_H
    
    #include "uow_graphics.h"
    #include "drawing.h"
    #include "board.h"
    #include <cmath>
    
    struct Point{
       int x;
       int y;
    
    }
    
    
    struct Gemstone {
    
    	struct Point *position;
    	struct Polygon *gemstone;
    	//with respect to the size of a cell
    	double size;
    	//score value
    	int value;
    
    
    };
    bool draw_gemstones(struct PacmanBoard* pcmb_p);
    bool init_gemstones(struct Gemstone *gem_p,struct PacmanBoard* pcmb_p,double size,int x,int y);
    bool construct_gemstone(struct Gemstone *gem_p);
    
    #endif

    the main problem is; im basically trying to plot something on a grid (draw a picture at a certain location in a window)...im passing my x and y values (1 and 0,which corrispond to coordinates on the grid) to state where i would like plot this object, the x and y values are used in a calculation(convert from location in matrix,to location in cartesian plane) to get me exact coordinates. ideally give x=1 and x=0 i should end up with something like x=-0.87 and y=0.93 ..but instead, the x and y are somehow not being passed as 1 and 0, they are being passed as 2 very large equal numbers, and there for my location on the plane is incorrect...there really isnt any need for print f statements,because my debugger gives the values of each items at each point in the program..and i stick cant find the problem

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Cut all the crap out, and put it in a small program to test your functions one at a time, to make sure they work right.
    Code:
    struct Point{
       int x;
       int y;
    
    };
    
    
    struct Gemstone {
    I assume you free-hand typed that, because otherwise you're missing a semi-colon there.

    You should print out gem_p->position->x and gem_p->position->y first. My guess is you just forgot to set them to 0 before you started screwing with them. Oh, and they're ints, so no matter what, you're still going to end up with ints.


    Quzah.
    Last edited by quzah; 12-07-2009 at 09:22 PM.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    ya the struct for point was in a different header file, so when i did my reply i just through that in
    ..and when you say print them first, do you mean before the call to init_gemston()?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Before, during, after. You're can't really go wrong.
    Code:
    bool init_gemstones(struct Gemstone* gem_p,struct PacmanBoard* pcmb_p,double size,int x,int y){
        if( gem_p )
        {
            printf("gem_p: ");
            if( gemp_p->position )
                printf( "x: %d, y: %d\n", gem_p->position->x, gem_p->position->y );
        }
        else
            printf("You dun ****** up!\n");
    
    
    	//get dimensions of individual cell
    	double cell_w = pcmb_p->width / (double)(pcmb_p->ncells_x);
    	double cell_h = pcmb_p->height / (double)(pcmb_p->ncells_y);
    
    	struct Point* points = (struct Point*)malloc(4*sizeof(Point));
    
    	//find center position of cell
    	gem_p->position->x=-1+(x*cell_w)+(cell_w/2); //BREAK POINT
    	gem_p->position->y=1-(y*cell_h)+(cell_h/2);
    
    	points[0].x=gem_p->position->x-cell_w*(size/2);
    	points[0].y=gem_p->position->y;					
    	
    	points[1].x=gem_p->position->x;
    	points[1].y=gem_p->position->y+cell_h*(size/2);
    
    	points[2].x=points[0].x+size*cell_w;
    	points[2].y=points[0].y;
    
    	points[3].x=points[1].x;
    	points[3].y=points[1].y-size*cell_h;
    
    	init_Polygon(gem_p->gemstone, gem_p->position, 4, points, 1.0, 1.0, 1.0);
    
    return true;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    i appologize if im confusing you guys, the code segments are just small segments of a very large project that consists of several files...the details of the actual program, im not sure are important...ill try to break down what my problem is again with a much more simple example

    this is what i want to happen:
    Code:
    function1(){
    
         function2(1,0);    //call function2 with hardcoded parameters
    
    }
    function2(int x,int y){
    
       perform actions on:1
       perform actions on:0
    }
    whats happening is:
    Code:
    function1(){
    
         function2(1,0);    //call function2 with hardcoded parameters
    
    }
    function2(int x,int y){
    
       perform actions on:2938474
       perform actions on:2938474
    }

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    the only issue with that, is this is all graphics and drawings of simple polygons, print statments actually dont work, so i use a debugger with a breakpoint in that location to read the values of x and y at that time...and at that point in the code they are large equal numbers...that i dont like

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    ok, so i figured out how to incorporate a printf into my code, and its giving me the same thing as the debugger

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by paret View Post
    whats happening is:
    Code:
    function1(){
    
         function2(1,0);    //call function2 with hardcoded parameters
    
    }
    function2(int x,int y){
    
       perform actions on:2938474
       perform actions on:2938474
    }
    I doubt it very much. Do this then:
    Code:
    x = 1;
    y = 0;
    printf("calling fun2 with: x: %d, y: %d\n", x, y );
    function2( x, y );
    ...
    
    
    void function2( int x, int y )
    {
        printf("in fun2: x: %d, y: %d\n", x, y );
        ...
    It's highly improbable that between the time you call the function, and the time it actually calls the function (a micro second), that it's corrupting the values passed. It's more likely that you're passing an address (which is what the large numbers look like) rather than a value, or something like that. I would advice doing what I showed above in addition to upping your warning level. (Put it first, just as I've shown it, not after you've done a bunch of stuff in the function.)

    Oh, and you keep compiling as C++, not C.



    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    can anyone offer any other solution?..PLEASE! im desperate, its been a week now, and i still havent resolved this problem

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Quote Originally Posted by paret View Post
    can anyone offer any other solution?..PLEASE! im desperate, its been a week now, and i still havent resolved this problem
    Did you do what quzah suggested? What was the result?
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    yes I tried quzahs suggestion and nothing changed..i think i narrowed it down to a problem with defrefercing a struct pointer to struct pointer...
    im trying to fill the value of gem_p->position->x and gem_p->position->y when i debug it tells me theres an error referencing those value

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-11-2008, 02:42 AM
  2. Replies: 1
    Last Post: 03-31-2008, 05:53 PM
  3. Problem with passing structs to a function.
    By darsh1120 in forum C Programming
    Replies: 7
    Last Post: 03-11-2008, 04:36 AM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. problem with passing data, again.
    By vnrabbit in forum C Programming
    Replies: 3
    Last Post: 10-22-2002, 02:14 PM

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