What is Single Dimension Arrays
Single-Dimension Arrays
The general form for declaring a single-dimension array is
type var_name[size];
Like other variables,arrays must be explicitly declared so that the compiler may allocate space for them in memory. Here, type declares the base type of the array, which is the type of each element in the array, and size defines how many elements the array will hold. For example, to declare a 100-element array called balance of type double, use this statement:
double balance[100];
An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example,
balance[3] = 12.23;
assigns element number 3 in balance the value 12.23.
In C/C++, all arrays have 0 as the index of their first element. Therefore, when you write
char p[10];
you are declaring a character array that has ten elements, p[0] through p[9]. For example, the following program loads an integer array with the numbers 0 through 99:
#include
int main(void)
{
int x[100]; /* this declares a 100-integer array */
int t;
/* load x with values 0 through 99 */
for(t=0; t<100; ++t) x[t] = t;
/* display contents of x */
for(t=0; t<100; ++t) printf(”%d “, x[t]);
return 0;
}
The amount of storage required to hold an array is directly related to its type and size. For a single-dimension array, the total size in bytes is computed as shown here:
total bytes = sizeof(base type) x size of array
C/C++ has no bounds checking on arrays. You could overwrite either end of an array and write into some other variable’s data or even into the program’s code. As the programmer, it is your job to provide bounds checking where needed. For example, this code will compile without error, but is incorrect because the for loop will cause the array count to be overrun.
int count[10], i;
/* this causes count to be overrun */
for(i=0; i<100; i++) count[i] = i;
Single-dimension arrays are essentially lists of information of the same type that are stored in contiguous memory locations in index order. For example:
char a[7];
Element a[0] a[1] a[2] a[3] a[4] a[5] a[6]
Address 1000 1001 1002 1003 1004 1005 1006
A seven-element character array beginning at location 1000
Generating a Pointer to an Array
You can generate a pointer to the first element of an array by simply specifying the array name, without any index. For example, given
int sample[10];
you can generate a pointer to the first element by using the name sample. Thus, the following program fragment assigns p the address of the first element of sample:
int *p;
int sample[10];
p = sample;
You can also specify the address of the first element of an array using the & operator. For example, sample and &sample[0] both produce the same results. However, in professionally written C/C++ code, you will almost never see &sample[0].
Passing Single-Dimension Arrays to Functions
In C/C++, you cannot pass an entire array as an argument to a function. You can, however, pass to the function a pointer to an array by specifying the array’s name without an index. For example, the following program fragment passes the address of i to func1():
int main(void)
{
int i[10];
func1(i);
.
.
.
}
If a function receives a single-dimension array, you may declare its formal parameter in one of three ways: as a pointer, as a sized array, or as an unsized array. For example, to receive i, a function called func1() can be declared as
void func1(int *x) /* pointer */
{
.
.
.
}
or
void func1(int x[10]) /* sized array */
{
.
.
.
}
or finally as
void func1(int x[]) /* unsized array */
{
.
.
.
}
All three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received. The first declaration actually uses a pointer. The second employs the standard array declaration. In the final version, a modified version of an array declaration simply specifies that an array of type int of some length is to be received. As you can see, the length of the array doesn’t matter as far as the function is concerned because C/C++ performs no bounds checking. In fact, as far as the compiler is concerned,
void func1(int x[32])
{
.
.
.
}
also works because the compiler generates code that instructs func1() to receive a pointer—it does not actually create a 32-element array.
Popularity: 1% [?]