An array contains a fixed number of data items with the same type.
The data items in an array are referred to as elements.
The following code declares an array type variable.
We placed a
number between square brackets []
following the name.
long numbers[10];
The number between square brackets defines how many elements the array contains.
It is called the array dimension.
Array index values start from zero, not one.
The following code shows how to average ten grades stored in an array.
#include <stdio.h>
/* www. ja va2 s. com*/
int main(void)
{
int grades[10]; // Array storing 10 values
unsigned int count = 10; // Number of values to be read
long sum = 0L; // Sum of the numbers
float average = 0.0f; // Average of the numbers
printf("\nEnter the 10 grades:\n"); // Prompt for the input
// Read the ten numbers to be averaged
for(unsigned int i = 0 ; i < count ; ++i)
{
printf("%2u> ",i + 1);
scanf("%d", &grades[i]); // Read a grade
sum += grades[i]; // Add it to sum
}
average = (float)sum/count; // Calculate the average
printf("\nAverage of the ten grades entered is: %.2f\n", average);
return 0;
}
The code above generates the following result.
We can define an array using [] syntax.
For instance, we define array of int and char.
int numbers[5]; char chars[10];
We also can construct array from struct.
struct employee{ int id; char name[10]; char country[5]; }; struct employee list[5];
The following code displays all values in an array.
You can access array element values by index.
#include <stdio.h>
/*from w w w.ja va 2 s . co m*/
int main(void)
{
int grades[10];
unsigned int count = 10; // Number of values to be read
long sum = 0L; // Sum of the numbers
float average = 0.0f; // Average of the numbers
printf("\nEnter the 10 grades:\n"); // Prompt for the input
// Read the ten numbers to be averaged
for(unsigned int i = 0 ; i < count ; ++i)
{
printf("%2u> ",i + 1);
scanf("%d", &grades[i]); // Read a grade
sum += grades[i]; // Add it to sum
}
average = (float)sum/count; // Calculate the average
for(unsigned int i = 0 ; i < count ; ++i) {
printf("\nGrade Number %2u is %3d", i + 1, grades[i]);
}
printf("\nAverage of the ten grades entered is: %.2f\n", average);
return 0;
}
The code above generates the following result.
After declared an array, we can set and get data on array.
#include <stdio.h>
/*from w ww . j a v a 2 s. c o m*/
struct employee{
int id;
char name[10];
char country[5];
};
int main() {
// define array
int numbers[5];
char chars[10];
struct employee list[5];
// insert data
int i;
for(i=0;i<5;i++){
numbers[i] = i;
list[i].id = i;
sprintf(list[i].name,"usr %d",i);
sprintf(list[i].country,"DE");
}
sprintf(chars,"hello c");
// display data
for(i=0;i<5;i++){
printf("%d %c\n",numbers[i],chars[i]);
printf("struct. id: %d, name: %s, country : %s \n",
list[i].id,list[i].name,list[i].country);
}
printf("%s\n",chars);
return 0;
}
The code above generates the following result.
The address of operator, &, returns the memory address for its operand.
The following code shows how to output the address of some variables:
#include<stdio.h>
/* ww w . j a v a 2 s .com*/
int main(void)
{
// Define some integer variables
long a = 1L;
long b = 2L;
long c = 3L;
// Define some floating-point variables
double d = 4.0;
double e = 5.0;
double f = 6.0;
printf("A variable of type long occupies %u bytes.", sizeof(long));
printf("\nHere are the addresses of some variables of type long:");
printf("\nThe address of a is: %p The address of b is: %p", &a, &b);
printf("\nThe address of c is: %p", &c);
printf("\n\nA variable of type double occupies %u bytes.", sizeof(double));
printf("\nHere are the addresses of some variables of type double:");
printf("\nThe address of d is: %p The address of e is: %p", &d, &e);
printf("\nThe address of f is: %p\n", &f);
return 0;
}
The code above generates the following result.
Here's a declaration for an array with four elements:
long number[4];
The array name, number
, identifies the memory address for the array.
The index value represents the offset of array element memory address.
The following code sets a value for each element in an array and outputs the address and contents of each element:
//from www .j a v a 2s . c o m
#include <stdio.h>
int main(void)
{
int data[5];
for(unsigned int i = 0 ; i < 5 ; ++i)
{
data[i] = 12*(i + 1);
printf("data[%d] Address: %p Contents: %d\n", i, &data[i], data[i]);
}
return 0;
}
The code above generates the following result.
To initialize the elements of an array, specify the initial values between braces and separate them by commas in the declaration.
For example:
double values[5] = { 1.5, 2.5, 3.5, 4.5, 5.5 };
If there are fewer initializing values than elements, the elements without initializing values will be set to 0. Thus, if you write:
double values[5] = { 1.5, 2.5, 3.5 };
The first three elements will be initialized with the values between braces, and the last two elements will be initialized with 0.
To initialize an entire array to zero, supply one element with the value of 0:
double values[5] = {0.0};
The entire array will then be initialized with 0.0.
You can omit the size of the array when you specify a list of initial values.
The compiler will assume that the number of elements is the number of values in the list:
int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
The size of the array is determined by the number of initial values in the list.
The sizeof
operator computes the number of bytes that a variable occupies.
You can apply the sizeof
operator to a type name like this:
printf("The size of a variable of type long is %zu bytes.\n", sizeof(long));
The sizeof operator works with arrays too.
Suppose that you declare an array with the following statement:
double values[5] = { 1.5, 2.5, 3.5, 4.5, 5.5 };
You can output the number of bytes that the array occupies with the following statement:
printf("The size of the array, values, is %zu bytes.\n", sizeof values);
This will produce the following output:
The size of the array, values, is 40 bytes.
Thus you can use the sizeof
operator to calculate the number of elements in an array:
size_t element_count = sizeof values/sizeof values[0];
element_count
is of type size_t
since sizeof operator produces size_t type value.
You can apply the sizeof operator to a data type, the following code returns the array dimension for double type array.
size_t element_count = sizeof values/sizeof(double);
The full source code for calculating the array length or dimension is as follows.
#include <stdio.h>
//from w w w . j av a 2s. c om
int main(void)
{
double values[5] = { 1.5, 2.5, 3.5, 4.5, 5.5 };
size_t element_count = sizeof(values)/sizeof(values[0]);
printf("The size of the array is %zu bytes ", sizeof(values));
printf("and there are %u elements of %zu bytes each\n", element_count, sizeof(values[0]));
return 0;
}
The code above generates the following result.
You can use the sizeof operator when you use a loop to process all the elements in an array.
For example:
#include <stdio.h>
//w ww . j ava 2 s . c om
int main(void)
{
double values[5] = { 1.5, 2.5, 3.5, 4.5, 5.5 };
double sum = 0.0;
for(unsigned int i = 0 ; i < sizeof(values)/sizeof(values[0]) ; ++i){
sum += values[i];
}
printf("The sum of the values is %.2f", sum);
return 0;
}
The code above generates the following result.