1

I was doing some training course, and this question still has my attention.

Question:

Declare an array of type float named math_constants. The array should be big enough to hold 2 numbers.

Why is

float math_constants[2];

Correct?

And why is

float math_constants[1];

Wrong?

It might be a newbie mistake, but its confusing me.

5
  • 2
    Why do you think your answer is correct? Commented May 9, 2014 at 7:35
  • Because an array starts counting at 0 Commented May 9, 2014 at 7:37
  • 1
    @Tareffic Your question is not specific to iOS, that's why the tag has been removed. Commented May 9, 2014 at 7:41
  • @NikolaiRuhe i added it, because i was following an iOS course Commented May 9, 2014 at 7:42
  • Now the misunderstanding is clear. The integer in brackets is the size (i.e. the number of elements) of that array, not its upper limit of indexes. Commented May 9, 2014 at 7:42

2 Answers 2

4

The number within square brackets is the size of the array, i.e., the number of elements in the array. This is different from the fact that indexing of the elements starts from 0.

Sign up to request clarification or add additional context in comments.

Comments

0

c syntax for array

arrayType array_name[SIZE_OF_ARRAY]

in the case of your example math_constants[1] can hold 1 value, namely when you try to access math_constants[0]

similarly , math_constants[2] can hold 2 value, math_constants[0] and math_constants[1]

2 Comments

Your syntax example is confusing. You probably wanted to explain how to define an array but you omitted the type.
I forgot to include that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.