Can't get code to work properly

Member
Posts: 75
Joined: 2009.01
Post: #1
I'm not entirely sure I'm doing this right.
Code:
typedef struct
    {
        int a[3];
    } SomeType;

SomeType lala;


- (void)setVertices:(SomeType)v :(int)x :(int)y :(int)z {

    v.a[0] = x;
    v.a[1] = y;
    v.a[2] = z;
}


- (void)test {

    [self setVertices:lala :1 :3 :5];
    printf("%i, %i, %i", lala.a[0], lala.a[1], lala.a[2]);
}

Shouldn't it be printing out 1, 3, and 5?
It prints out 0, 0, 0.
Any help on this matter Sad
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
Nope. In the code you pasted, -setVertices:::: is modifying a local copy of a SomeType, since that's the way you're passing it. The SomeType in -test remains unaltered.
Quote this message in a reply
Member
Posts: 75
Joined: 2009.01
Post: #3
Is there anyway to fix it so that the values are assigned?
I have a different piece of code working fine but it's like this:

Code:
typedef float MapVertex[3];
typedef float MapTexCoord[2];
MapVertex mapVerticesLayer1[15000];
MapTexCoord mapTexCoords[15000];
MapVertex mapVerticesLayer2[15000];
MapTexCoord mapTexCoordsLayer2[15000];


- (void)setMapVertices:(MapVertex)v :(float)x :(float)y :(float)z {
    
    v[0] = x;
    v[1] = y;
    v[2] = z;
}


- (void)setMapTexCoords:(MapTexCoord)v :(float)x :(float)y {
    
    v[0] = x;
    v[1] = y;
}
This works fine but I cannot seem to "reset" it all back to 0 and I'm having problems debugging it with NSLog and printf, so I decided to change it around a bit.

EDIT:
Well, I moved the printf function inside of -setVertices and it seems like it works =/
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #4
jeonghyunhan Wrote:EDIT:
Well, I moved the printf function inside of -setVertices and it seems like it works =/

Of course, because you're printing out the function's local copy of the struct. It sounds like you may need to brush up on some C fundamentals. I'd give you the answer, but you're better off reaching it by your own understanding, since this would cripple you in future coding.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  How to get part of code to work jeonghyunhan 2 3,000 Feb 12, 2009 09:31 AM
Last Post: ThemsAllTook