-2

I have an variable with the datatype CGPoint, named endPosition. When endPosition gets its value inside an if statement, it returns this crazy value: End position: {1.6347776e-33, 1.4012985e-45}.

1. example:

    if ([touch view] != background)
    {
        CGPoint location = [touch locationInView:self.view];

        CGPoint endPosition;

        if([touch view] == circle){
            CGPoint endPosition = {462.5, 98.5};
        }

        CGFloat xDist = (endPosition.x - location.x);
        CGFloat yDist = (endPosition.y - location.y);

        CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));

        NSLog(@"End Position: %@", NSStringFromCGPoint(endPosition));
    }

When the CGPoint endPosition is not inside this if statement, i get the right value: End position: {462.5, 98.5}

2. example:

    if ([touch view] != background)
    {
        CGPoint location = [touch locationInView:self.view];

        CGPoint endPosition = {462.5, 98.5};

        CGFloat xDist = (endPosition.x - location.x);
        CGFloat yDist = (endPosition.y - location.y);

        CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));

        NSLog(@"End Position: %@", NSStringFromCGPoint(endPosition));
     }

Can anyone tell me what to do? I need this if statement :) Thanks in advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
thar
  • 1,094
  • 2
  • 13
  • 25

4 Answers4

4

In your example 1, you never initialise the value of endPosition. That's because in the 'if' statement (if([touch view] == circle){) you are defining a new variable called endPosition which replaces the other one in that scope. You should probably initialise endPosition to CGPointZero anyway.

Wain
  • 118,658
  • 15
  • 128
  • 151
2

That's because in the first case you don't set a value for endPoint if [touch view] != circle.

In that case your variable is uninitialized and you get a random value that happens to be there in memory. You have to handle the other case (else) or initialize your variable to some value when you declare it, like CGPointZero.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
0
    CGPoint endPosition; //This is a declaration of a of new stack variable of name "endPosition"

    if([touch view] == circle){
        CGPoint endPosition = {462.5, 98.5}; //...AND this is a declaration of another variable
    }

You want to remove CGPoint from the second line.

Additionally, since your CGPoint is never initialized it's not necessarily the case that it will have a non-garbage value. You could add an else block and put endPosition = CGPointZero there or you could do that on the first line.

EDIT: Also {462.5, 98.5} is the wrong size (2 doubles), {462.5f, 98.5f} is 2 floats, but you should probably just stick to CGPointMake and avoid 'complex' literals.

Ryan Dignard
  • 639
  • 1
  • 5
  • 16
  • Hi Ryan, It makes sense. But when i try to do it, like this: CGPoint sp = CGPointZero; if([touch view] == circle){ sp = {462.5, 98.5}; // Expected expression } I get this error expected expression? – thar Dec 12 '13 at 08:30
  • Try turning {462.5, 98.5} into CGPointMake(462.5f, 98.5f) – Ryan Dignard Dec 14 '13 at 23:26
0

Solution:

    CGPoint endPosition = CGPointZero;

    if([touch view] == circle){
        endPosition = CGPointMake(462.5, 98.5);
    }
thar
  • 1,094
  • 2
  • 13
  • 25