Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

swathen

macrumors newbie
Original poster
Feb 2, 2011
3
0
Hello all! I have been out of programming for years, but have a lot of (old) experience programming in C, C++ and Java. I just got back into it and am realizing quickly that Objective C is a bit of a different animal. Anyway to the question at hand.

In one of my methods I have the following lines of code:
Code:
cpVect verts1[] = { cpv(0.0, 0.0), cpv(50.0, 0.0), cpv(45.0, -15.0), cpv(0.0, -15.0) }; 

cpVect verts2[] = { cpv(0.0, 0.0), cpv(50.0, 0.0), cpv(45.0, -15.0), cpv(0.0, -15.0) }; 

cpVect slab[] = { cpv(0.0, 0.0), cpv(50.0, 0.0), cpv(45.0, -15.0), cpv(0.0, -15.0) }; 

//cpVect and CGPoint are the same thing by the way.
Updating this array directly:

Code:
verts1[0].x = 5.0;
verts1[0].y = 5.0;
works like a champ.

What I want to do though is pass these arrays of structs as parameters to a function that will then update them with new values.

I can't seem to get pass by reference to work.

With the following function dec:

Code:
-(BOOL) buildHouse: (cpVect **)verts1 : (cpVect **)verts2 : (cpVect **) hori
{
    verts1[0]->x = 5.0

    return(FALSE);
}
and the function call from the main code as follows:

Code:
while ([self buildHouse:&vertsWall1 :&vertsWall2 :&slab]) 
{

}
I get a warning that I am passing an incompatible pointer type.

I have tried just about everything I can think of to get this to work. I know it is something simple, but I have been looking at this too long.

Thanks in advance for the help!!!!
 
Last edited by a moderator:
My first advice is that you will regret using C style arrays like this. In general they suck, except in certain circumstance, unless you really know what you're doing. If your arrays are fixed size then wrap them in a struct or class. If they're not fixed size, well don't say I didn't warn you.

Next, use named parameters in Objective-C. Like this:

Code:
-(BOOL)buildHouseWithVerts:(cpVect **)verts1 verts2:(cpVect **)verts2 andHori:(cpVect **)hori;

While the way you're doing it is legal it's non-idiomatic and can easily get you into trouble if you don't really know what you're doing.

Next, a pointer to an array is the same as a pointer to the first element. Anyway, something like this should work:

Code:
-(BOOL)buildHouseWithVerts:(cpVect *)verts1 verts2:(cpVect *)verts2 andHori:(cpVect *)hori;

-(BOOL)-(BOOL)buildHouseWithVerts:(cpVect *)verts1 verts2:(cpVect *)verts2 andHori:(cpVect *)hori
{
verts1[0].x = 5.0

return(NO);
}

and the function call from the main code as follows:

while ([self buildHouseWithVerts:vertsWall1 verts2:vertsWall2 andHori:slab]) 
{

}

Obviously not run through the compiler.
 
Thanks for the replies. I'll give this a shot.

The reason I am using the C style arrays is because I am writing an app using Cocos2d and Chipmunk. The Chipmunk functions I am using require it.

Thanks again!

EDIT:

I made the changes and it compiles and runs fine.

The problem of updating the array remains however.

Code:
verts1[0].x = 5.0;

Doesn't change verts1[0].x

It is behaving exactly like I would expect it to if I passed verts1 by value. But my code now matches your example exactly, we are dealing in pointers, and I am at a loss as to why when the function returns, verts1[0].x has not updated.

Thanks again for helping with this!
 
Last edited by a moderator:
I give up. :confused: I am still not sure why I was unable to get this to work as is, but I couldn't.

The solution I implemented (as suggested) was to simply put an array class wrapper around the verts. Then I modified my function to accept pointers to the arrays. I then performed my updates to the vertices via the object wrapper.

Works like a champ! :cool:

Thanks again for the responses. :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.