Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Pointers and Memory Array Size and Length

Fabrizio Rinaldi
Fabrizio Rinaldi
5,359 Points

Array_Length

float real_numbers [] = {1.11, 1.12, 1.13, 1.14, 1.15};

printf ("Array real_numbers can store %ld items.", sizeof(real_numbers)÷sizeof(float));

i can't find the mistake in this part of code, anyone can help me?

7 Answers

Hi Fabrizio,

You want to use the forward slash for the division operator / You've got a division symbol in there instead.

sizeof(real_numbers) / sizeof(float)

Andrew Pham
Andrew Pham
3,492 Points

Is it possible that the result of 'sizeof(real_numbers)÷sizeof(float)' returns an integer, hence the string formatting '%ld' (long) is incorrect? Try changing '%ld' to '%d'

Fabrizio Rinaldi
Fabrizio Rinaldi
5,359 Points

ok, i'll do that! thank you Andrew ;)

Fabrizio Rinaldi
Fabrizio Rinaldi
5,359 Points

Task:

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.

Solution:

float real_numbers [] = {1.1, 1.2, 1.3, 1.4, 1.5};

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

Error:

Make sure you are calling printf and passing a string (not an NSString) and real_numbers as the parameters. The correct format option is '%ld'.

I can't find the mistake and it continues to say that's equal to the exercise i've done before.

For andrew: is right to write %ld, cuz i've tried with %d and gives me an error.

Did you recycle the printf statement from task 2 when doing task 3? You should keep previous code in place if that's what happened.

Fabrizio Rinaldi
Fabrizio Rinaldi
5,359 Points

I deleted printf("Array real_numbers is %ld bytes.", sizeof(real_numbers)); and shown me that error.

Now i completed the exercise ;)

Andrew Pham
Andrew Pham
3,492 Points

Yes agreed with Jason. Your final code should be: float real_numbers[5]; printf("Array real_numbers is %ld bytes.", sizeof(real_numbers)); printf("Array real_numbers can store %ld items.", sizeof(real_numbers) / sizeof(float));