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 Objective-C Basics (Retired) Pointers and Memory Array Size and Length

Confused...

Question: 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.

My answer:

float real_numbers[5]; printf("Array real_numbers is %ld bytes.\n",sizeof(real_numbers)); printf("Array real_numbers can stroe %ld items.\n",(sizeof(real_numbers));

help Please!!

7 Answers

Rodrigo Chousal
Rodrigo Chousal
16,009 Points

The question is in fact asking for what you wrote. It should be:

printf("Array real_numbers can store %ld items.\n", sizeof(real_numbers)/sizeof(float));
Andres Mauricio Guerrero
Andres Mauricio Guerrero
4,111 Points

help in what? understanding? code not working?

if your'e getting a syntax error (because I copy-Pasted your code, on this line: printf("Array real_numbers can stroe %ld items.\n",(sizeof(real_numbers));

your'e missing a closing parentheses so at the end of the line it should look like this (sizeof(real_numbers)));

Rodrigo Chousal
Rodrigo Chousal
16,009 Points

sizeof() doesn't need a parenthesis holding it, so you should just write: sizeof(real_numbers)); instead of: (sizeof(real_numbers)));

Andres Mauricio Guerrero
Andres Mauricio Guerrero
4,111 Points

ohh ok, well I learned in college that a float normally has a size of 4 bytes (or 32 bits which is 8*4 bytes) so whats going on here is that since your array of floats goes up to 5 (real_numbers[5]), the you multiply 5*4 bytes (thats why in the first printf you get 20 bytes ), then what these guys are writing... you see that they're doing a division, so that division is between the 20 bytes that you just got from the first multiplication, divided by the size of a float which is 4 from what we just said...thats where you get the 5 which is the amount of items you cans store in that array.

Hope it helps... basically what you are doing is simplifying a fraction (5*4)/4 = 5 cheers ;)

Chad Goodyear
Chad Goodyear
9,634 Points

...and you spelt 'store' wrong ;o)

Thank you guys for your replies!! I Andres: I guess I need help with the concept. In the example in the video, the instructor (if forget his name right now) found the length by dividing the variable named by the float by the float itself: printf("Array real_numbers can store %ld items", sizeof(real_numbers)/sizeof(float));

i dont know if the question is asking for that or something else.

Chad Goodyear
Chad Goodyear
9,634 Points

Your code: printf("Array real_numbers can stroe %ld items.\n",(sizeof(real_numbers));

The code you quote the instructor as using: printf("Array real_numbers can store %ld items", sizeof(real_numbers)/sizeof(float));

So you get that you made a typo on 'store' and missed a 'divided by' bit?

Do you want me to run through the concept (why your doing the device bit)?