Thread: Passing Structure Pointer

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    8

    Passing Structure Pointer

    I am having trouble passing this structure with pointers. Im using recursion to implement a marching cubes algorithm. When surrounding cubes is called, it checks each cube on each of the six faces of the cube to see if they have any matching vertices (meaning they are connected). If this is true, surrounding cubes is called again on that cube and so forth. The function checkCube checks if the cube has matching vertices and is in range as well as creates a GRIDCELL(cube) structure with the given data. Im having trouble with the pointers though. It all compiles correctly but whenever it finds a cube its vertices are all wrong. They are correct in checkCube but when it gets back into surrounding cubes they are all 0. I think I am just using the wrong syntax to pass the GRIDCELL structure.

    Basically I want to be in surroundingCubes, call checkCube and return a GRIDCELL structure that can be used in another function call.


    I only attached code I thought was relevant as the program is already 1000 lines so hopefully this is enough information.


    Thanks!




    Code:
    typedef struct {
       XYZ p[8];
       double val[8];
    } GRIDCELL;
    
    static void surroundingCubes(GRIDCELL cube, int w, int t, int *flags)
    {
    	int ntri;
    	int index;
    	GRIDCELL *grid= (GRIDCELL*)malloc(sizeof(GRIDCELL));
    
    	if (checkCube(giCubeLength, (cube.p[1].x) + (giCubeLength-1), cube.p[1].y, cube.p[1].z, t, w, *grid)==true)
    	{
    		surroundingCubes(*grid, w, t, flags);	
    	}
    ...
    ...
    ...
    }
    
    static bool checkCube (int cubeLength, int startX, int startY, int startZ, int t, int w, GRIDCELL grid)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you expect that last parameter to checkCube to change when you call the function? Why? (Hint: functions in C can never modify the actual parameters passed to them such that the calling function can notice.)

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    8
    i thought because it was a pointer it could be modified. it works because the values are correct if I printf the values before i call surrounding cubes but once it gets into surrounding cubes all the values are reset at zero.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The last argument to checkCube is just a plain old GRIDCELL, no pointer involved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass pointer to a structure in a message queue
    By smitsaboo in forum C Programming
    Replies: 4
    Last Post: 08-15-2009, 11:33 PM
  2. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. passing pointer to structure to a function
    By samual_van in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2005, 07:18 PM