12.4 Array Element Assignment

12.4.1 Synopsis

array [index] = expression;

12.4.2 Description

If element number index exists, its value is replaced by expression. If such element doesn't exist, it is created and expression is stored within; the gap between the last element and the new element is filled with empty elements.

As usual, expression can be any valid expression.

12.4.3 Example

cows_array [ ] = { "I haven't bought any leather articles",
                   "for a very long time.",
                   "My ideal is to be able to avoid" };

An array called cows_array, consisting of three strings is created. These are the elements and their values:

cows_array [0]

I haven't bought any leather articles

cows_array [1]

for a very long time.

cows_array [2]

My ideal is to be able to avoid

Now, we set cows_array [5]; the array is too short so will automatically grow and empty strings will fill the new elements.

cows_array [5] = "-- Martina Navratilova";
cows_array [0]

I haven't bought any leather articles

cows_array [1]

for a very long time.

cows_array [2]

My ideal is to be able to avoid

cows_array [3]

Empty string

cows_array [4]

Empty string

cows_array [5]

- Martina Navratilova

Finally, we set cows_array [3] and cows_array [4]; the new definitions will simply replace old ones.

cows_array [3] = "all animal products,";
cows_array [4] = "in food as well as clothing.";
cows_array [0]

I haven't bought any leather articles

cows_array [1]

for a very long time.

cows_array [2]

My ideal is to be able to avoid

cows_array [3]

all animal products,

cows_array [4]

in food as well as clothing.

cows_array [5]

- Martina Navratilova

This manual can be downloaded from http://www.g-cows.org/.