Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Code challenge: Array length

Hi, this is my code and the question is: Print the length of the array real_numbers using the sizeof() function. Your output should look like the following: Array real_numbers can store x items. And I have no idea where I went wrong! P.S it says Bummer! That was not the correct string. Do you have the correct format character? sizeof(real_numbers) returns a long. That format character is '%ld'.

any help will be much appreciated!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

float real_numbers[] = {1.11,1.22,1.33,1.44,1.55}; printf("Array real_numbers is %ld bytes.\n",sizeof(real_numbers)); printf("Array real_numbers can store %ld items.",sizeof(real_numbers));

6 Answers

Try swapping %ld for %f I'd say?

dat didn't work:( though thanks heaps anyway!!!:)

Hi I have a similar issue. I have written your code into Xcode and it compiles successfully, but then on the treehouse iPad app it says "Somethings not right. Make sure array is defined and of type float."

I know SO frustrating!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

float real_numbers[5]; //we didn't have to explicitly set values!

printf("Array real_numbers is %ld bytes.\n", sizeof(real_numbers)); //gets the size of real_numbers array in bytes (which is going to be 5 * 4 as float values are 4 bytes of memory each)

printf("Array real_numbers can store %ld items.\n", sizeof(real_numbers)/sizeof(float)); //extracting the number of float items your real_numbers array can store!

I think mine worked like thus, however:

printf("Array real_numbers can store %ld items.\n", sizeof(real_numbers)/4);

Though this would be explicitly stating the size of a float variable (which I knew to be 4). The solution I posted up is more elegant.

THANKS HEAPS!!!!!!!!!!!!!!!!!!!!!!!! P.S WE BOTH HAVE THE SAME NAME!!!!!!!!!!!! AWESOME!!!!!:)

No worries, I'm glad I could help! And hi-five! Most Laurences/Lawrences I know are spelt with a w too!