trikker
trikker

Reputation: 2709

Array Allocation Subscript Number

Quick question regarding how memory is allocated.

If someone were to allocate 20 chars like this:

char store[20];

does this mean that it allocated 20 char type blocks of memory, or that it allocated char type blocks of memory starting with 0 and ending with 20. The difference is that the first example's range would be from store[0] to store[19], whereas the second example's range would be from store[0] to store[20].

Upvotes: 0

Views: 448

Answers (4)

Cameron
Cameron

Reputation: 98826

It means it allocated one block of memory large enough to hold 20 chars (from index 0 to 19)

Upvotes: 3

sepp2k
sepp2k

Reputation: 370397

It allocates memory for 20 chars, i.e. valid indices go from 0 to 19.

Upvotes: 0

David Thornley
David Thornley

Reputation: 57066

The first - char store[20] allocates 20 chars, from 0 to 19.

Upvotes: 0

ChristopheD
ChristopheD

Reputation: 116267

[0] to [19] (20 elements, that is)

Upvotes: 3

Related Questions