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 trialStan Shoemaker
616 PointsAnswer to Task 1 challenge is wrong!
The task is to declare an array named math_constants that can hold TWO elements. The statement: "float math_constants[1];" generates an error.
The only answer the challenge will accept is "float math_constants[2];", the declaration for a THREE element array.
4 Answers
Patrick Donahue
9,523 PointsWhen you declare an array in C you tell it how many slots you want it to have. When you insert an item into an array at an index it starts from 0.
Thomas Nilsen
14,957 PointsThink of it this way: An array contains values from index 0 to N-1 (N being the numbers of elements in the array), meaning that an array with 3 elements have the indices: 0, 1, 2 (N-1)
Stan Shoemaker
616 PointsPlease tell me why the declaration 'float math_constants[1]' will not create an array that will hold 2 numbers.
Thomas Nilsen
14,957 PointsBecause the way the syntax works is
float math_constants[number of elements in the array]
So, if you write 'float math_constants[1]'. The array contains ONE elements, which is essentially the same as creating a single float variable.