answersLogoWhite

0

The primary use of an array is create a collection of anonymous variables of the same type.

Although we can name all our variables at compile time, we can only do so if we know precisely how many variables we require at compile time. Even then, if the number of variables is large enough, naming and referring to each one by name can be impractical; imagine naming a million integer variables individually!

Arrays allow us to refer to a collection of variables through a single name; the array name. Every variable in an array must be of the same type. The variables are known as "elements" and the elements must be stored contiguously (one after the other). The array name serves as a reference to the start address of the array and thus to the first element of the array. All other elements are anonymous.

For any given type T, the type T[n] is an array of n elements of type T, where n is a constant. The number of elements in an array is limited only by available memory, bearing in mind that memory must be allocated as a single contiguous block. Arrays can also be disk-based however the same principals apply to both disk-based and memory-based arrays.

Consider the following declaration:

int x[1000];

The name x refers to an array of 1000 integer elements. The amount of memory physically allocated to this array is 1000 * sizeof (int) bytes. If we suppose that sizeof (int) is 4 bytes, the amount of memory allocated will be exactly 4000 bytes. The compiler can work this out from the declaration alone.

The compiler can also work out the address of every element within the array using a zero-based index. We use the subscript operator to refer to individual elements by index:

x[10] = 42;

Here, we've assigned the value 42 to the 11th element (index 10). The compiler calculates the address of the 11th element using trivial pointer arithmetic:

sizeof (int) * 10 + x;

Note that x, &x and &x[0] all refer to the same memory address (the address of the 1st element).

Arrays may be fixed-length or variable-length. We use fixed-length arrays when we know exactly how many elements we need at compile time. Fixed-length arrays may be allocated statically (in the program's data segment), on the call stack or on the heap (the free store). Variable-length arrays can only be allocated on the heap.

To create an array on the heap we need to use a pointer variable to keep track of the start address:

void f (const int elements) {

int* p1 = malloc (1000 * sizeof (int)); // fixed-length array on the heap

int* p2 = malloc (elements * sizeof (int)); // variable-length array on the heap

// use arrays...

p1[10] = 42;

// ...

free (p2);

free (p1);

}

Note that we can use the array subscript operator with any pointer type except void*. This is because the compiler needs to know the length of the type being pointed at, but sizeof (void) is meaningless.

Whenever we pass arrays to functions we must always pass the length of the array through a separate argument. This is because an array implicitly converts to a pointer to the first element and we cannot determine how many elements are actually being referred to from the pointer alone:

void f (int a[], unsigned len) {

for (int i=0; i<len; ++i) printf ("%d\n", a[i]);

}

The only exceptions to this are null-terminated arrays or arrays that use some pre-determined value to mark the end of the array. Character arrays (strings) are a typical example:

void g (char* str) {

printf ("%s", str); // Note: str must be null-terminated!

}

Note that, by convention, we use char* when referring to a null-terminated string rather than char[]. The latter is conventionally used to denote a non-null terminated string in which case we must also pass the string's length:

void h (char str[], unsigned len) { for (int i=0; i<len; ++i) printf ("%c", str[i]);

}

User Avatar

Wiki User

9y ago

What else can I help you with?

Continue Learning about Engineering

Differentiate single dimensional array to double dimensional array?

A single dimensional array is an array of items. A two-dimensional array is an array of arrays of items.


What is meant by irregular dimensional array?

An irregular dimensional array is a special type of multi-dimensional array.First we must understand that a multi-dimensional array is just an array of arrays. Each element in the array is, itself, an array of elements.A regular multi-dimensional array will be an array of size n, with each element containing a separate array of size m. That is, each sub-array has the same size.An irregular multi-dimensional array will be a multi-dimensional array in which each sub-array does not contain the same number of elements.Regular array:array[0] = new array{0, 1, 2}array[1] = new array{3, 4, 5}array[2] = new array{6, 7, 8}array[3] = new array{9, 10, 11}This regular array is an array of size 4 in which each sub-array is of size 3.Irregular array:array[0] = new array{0, 1, 2}array[1] = new array{3, 4}array[2] = new array{5, 6, 7}array[3] = new array{8, 9, 10, 11}This irregular array is an array of size 4 in which the size of each sub-array is not the same.


How do you swap two adjecent no in array in c?

Option 1) Use a temporary variable: int x = array[i]; array[i] = array[i+1]; array[i+1] = x; Option 2) Use bit operators: array[i] ^= array[i+1] ^= array[i];


How do you swap rows in 2 dimensional array in Java?

[]temp = array[1] array[2]=array[1] array[1]=[]temp


What is the array function in CAD?

there r 2 types of array in cad - rectangular array and polar array...........

Related Questions

Differentiate single dimensional array to double dimensional array?

A single dimensional array is an array of items. A two-dimensional array is an array of arrays of items.


What is meant by irregular dimensional array?

An irregular dimensional array is a special type of multi-dimensional array.First we must understand that a multi-dimensional array is just an array of arrays. Each element in the array is, itself, an array of elements.A regular multi-dimensional array will be an array of size n, with each element containing a separate array of size m. That is, each sub-array has the same size.An irregular multi-dimensional array will be a multi-dimensional array in which each sub-array does not contain the same number of elements.Regular array:array[0] = new array{0, 1, 2}array[1] = new array{3, 4, 5}array[2] = new array{6, 7, 8}array[3] = new array{9, 10, 11}This regular array is an array of size 4 in which each sub-array is of size 3.Irregular array:array[0] = new array{0, 1, 2}array[1] = new array{3, 4}array[2] = new array{5, 6, 7}array[3] = new array{8, 9, 10, 11}This irregular array is an array of size 4 in which the size of each sub-array is not the same.


How do you swap two adjecent no in array in c?

Option 1) Use a temporary variable: int x = array[i]; array[i] = array[i+1]; array[i+1] = x; Option 2) Use bit operators: array[i] ^= array[i+1] ^= array[i];


How do you swap rows in 2 dimensional array in Java?

[]temp = array[1] array[2]=array[1] array[1]=[]temp


What is array literal in as2?

An array literal is a comma-separated list of the elements of an array. An array literal can be used for initializing the elements of an array.


Why the array is starting from zero?

By design; it makes the compiler's work easier. 1-based array's addressing-function: Address (array, index) = Address (array) + (index-1)*Elemsize(array) 0-based array's addressing-function: Address (array, index) = Address (array) + index*Elemsize (array)


What is the array function in CAD?

there r 2 types of array in cad - rectangular array and polar array...........


Will an array element be deleted when you retrieve it from the array?

You cannot delete from an array.


What is sparce array?

sparse array is one which has contents lower than its maximum size, that is the array has free or empty locations....


Explain the Different types of array?

one dementional array and two dementional array


How will you find the location of an element of an array?

Basically, &amp;array[i]; That is, the memory location for an array object with index i. Or, you can do: (array + i);


Write a c program to reverse an array without using another array?

public static int[] reverseArray(int[] array) { int i = 0, j = array.length - 1; for (i = 0; i &lt; array.length / 2; i++, j--) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; }