-2

I need to be sure if the following is true.

If I want to declare an array with just one element in C that would be:

char array[1];

Right? And I would assign a value to it with index 0. For example:

array[0] = 'S';

And there has been no memory storage set aside for array[1]. Even though I declared the array with:

char array[1];

I'm asking because I find it a bit counterintuitive because arrays begin with index 0. So I think that I should declare an array with a single element like this:

char array[0];

and then go ahaead and assign a value like above:

array[0] = 'S';

But through a bit of testing that seems to be wrong to declare an one element array with:

char array[0];
user3542317
  • 193
  • 2
  • 12
  • 4
    Yeah TRUE. Well, why would you want a one element array instead of a char or an int variable? – Arjun Sreedharan Sep 12 '14 at 08:53
  • Note array index start from `0`. – Jayesh Bhoi Sep 12 '14 at 08:54
  • 2
    The number between the square brackets is the size of the array, i.e. the number of elements it should contain. That is not the same number as the index of the last element, since indexing starts at 0. – unwind Sep 12 '14 at 08:57
  • What is your question? – juanchopanza Sep 12 '14 at 08:58
  • @ArjunSreedharan I sometimes use one-element arrays when usage requires mostly pointers (and the code doesn't have as many `&` and `*`): `struct tm now[1]; time_t tzero = time(0); memmove(tm, localtime(&tzero), sizeof tm); printf("today is %d.\n", tm->tm_mday);` – pmg Sep 12 '14 at 09:00
  • There's a discussion about why array indices should start with 0 [here](http://programmers.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm) if you are thinking it's counterintuitive – Tyress Sep 12 '14 at 09:04
  • I think you should know [why array index start from `0`](http://stackoverflow.com/questions/7320686/why-does-the-indexing-start-with-zero-in-c) – Jayesh Bhoi Sep 12 '14 at 09:09

2 Answers2

1

An array with a size of 1 can hold exactly one element at index 0. An array with a size of 2 can hold exactly two elements at indexes 0 and 1. ... An array with a size of 10 can hold exactly ten elements at indexes 0 to 9.

An array with a size of 0 doesn't make sense. It couldn't hold any data.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
1

C is not intuitive, it is quite the opposite...

When declaring an array you type how many items it should contain. If you want 1 item, then you declare it as array[1]. Nothing strange there.

However, arrays are indexed starting from 0. So to access the first element, you would have to type array[0]. This actually does makes sense - forget that they taught you in school that 1 is the first number when counting, this is not mathematically true, 0 is actually the first one.

(Ultimately it boils down to this: any number can be described according to a certain formula. If you for example have the decimal number 123, it can be described as 1*10^2 + 2*10^1 + 3*10^0, where 1, 2 or 3 are the values of each digit, 10 is the decimal base and the power ^n is the index. This formula is used when converting between different bases, such as binary, hex, octal, as it is true for every base. And as you can see, the indexing starts from 0 for the least significant digit.)

Lundin
  • 195,001
  • 40
  • 254
  • 396