Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7642537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:12:55+00:00 2026-05-31T09:12:55+00:00

I am having difficulty understanding why the following code is giving me the numbers

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:12:56+00:00Added an answer on May 31, 2026 at 9:12 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some difficulty understanding why the output of the following code is not
I was given this starter code for a project, but am having difficulty understanding
I've inherited the following Perl code, and I'm having difficulty understanding what exactly the
I've been having some difficulty in understanding the source of a problem. Below is
I'm having some difficulty in understanding the following WHERE clause in a T-SQL (SQL
I'm following a tutorial on php, and am having difficulty getting this to work.
I'm a newbie to VB, and I am having difficulty understanding what the following
I am having difficulty understanding how exceptions are handled when code is fetched dynamically
Hi everyone I'm having difficulty understanding the following function structures. Could somebody walk me
I'm having a little difficulty understanding alias_method / alias_method_chain . I have the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.