1

I am having difficulty understanding why the following code is giving me the numbers below. Can anyone explain this conversion from float to int? (pCLocation is a CGPoint)

counter = 0;
pathCells[counter][0].x = pCLocation.x;
pathCells[counter][0].y = pCLocation.y;
cellCount[counter]++;    
NSLog(@"%@",[NSString stringWithFormat:@"pCLocation at:
 %f,%f",pCLocation.x,pCLocation.y]);
NSLog(@"%@",[NSString stringWithFormat:@"path cell 0: %i,%i",
pathCells[counter][cellCount[counter-1]].x,pathCells[counter][cellCount[counter]].y]);
2012-03-09 01:17:37.165 50LevelsBeta1[1704:207] pCLocation at: 47.000000,16.000000
2012-03-09 01:17:37.172 50LevelsBeta1[1704:207] path cell 0: 0,1078427648
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Naseiva Khan
  • 965
  • 1
  • 7
  • 15
  • 1
    What is the type of `pathCells`? – Lily Ballard Mar 09 '12 at 05:34
  • have you tried: NSLog(@"%@",[NSString stringWithFormat:@"path cell 0: %i,%i", pathCells[counter][0].x,pathCells[counter][0].y]); What does it prints? – Mrunal Mar 09 '12 at 05:38
  • Anywhere you are writing `NSLog(@"%@", [NSString stringWithFormat:XXX, a, b, c]);` you can (and should) replace it with `NSLog(XXX, a, b, c);`. – benzado Mar 09 '12 at 05:46
  • Is it because of this: (I am not sure about this) : http://stackoverflow.com/questions/1893490/difference-between-format-specifiers-i-and-d-in-printf – Mrunal Mar 09 '12 at 05:48

1 Answers1

3

Assuming your code is otherwise correct:

I think it would help you to understand how NSLog and other printf-style functions work. When you call NSLog(@"%c %f", a_char, a_float), your code pushes the format string and values onto the stack, then jumps to the start of that function's code. Since NSLog accepts a variable number of arguments, it doesn't know how much to pop off the stack yet. It knows at least there is a format string, so it pops that off and begins to scan it. When it finds a format specifier %c, it knows to pop one byte off the stack and print that value. Then it finds %f, so now it knows to pop another 32 bits and print that as a floating point value. Then it reaches the end of the format string, so it's done.

Now here's the kicker: if you lie to NSLog and tell it you are providing a int but actually provide a float, it has no way to know you are lying. It simply assumes you are telling the truth and prints whatever bits it finds in memory however you asked it to be printed.

That's why you are seeing weird values: you are printing a floating point value as though it were an int. If you really want an int value, you should either:

  1. Apply a cast: NSLog(@"cell.x: %i", (int)cell.x);
  2. Leave it a float but use the format string to hide the decimals: NSLog(@"cell.x: %.0f", cell.x);

(Alternate theory, still potentially useful.)

You might be printing out the contents of uninitialized memory.

In the code you've given, counter = 0 and is never changed. So you assign values to:

pathCells[0][0].x = pCLocation.x;
pathCells[0][0].y = pCLocation.y;
cellCount[0]++;

Then you print:

pathCells[0][cellCount[-1]].x
pathCells[0][cellCount[0]].y

I'm pretty sure that cellCount[-1] isn't what you want. C allows this because even though you think of it as working with an array of a specific size, foo[bar] really just means grab the value at memory address foo plus offset bar. So an index of -1 just means take one step back. That's why you don't get a warning or error, just junk data.

You should clarify what pathCells, cellCount, and counter are and how they relate to each other. I think you have a bug in how you are combining these things.

benzado
  • 82,288
  • 22
  • 110
  • 138
  • I should have clarified: pathCells is: CGPoint pathCells[50][50]; cellCounter and counter are both c int type. When I correct that -1 error and try: %f %f instead of %i %i I get the correct value, but using %i %i or even %d %d gives me the value above. – Naseiva Khan Mar 09 '12 at 11:06
  • UPDATE: I have change my data point to my own point structure: MyPoint with ints for x and y instead of floats, and I am now getting the correct result in nslog. My original question is still not clearly answered, however. – Naseiva Khan Mar 09 '12 at 11:31