Submitted by: Submitted by bbycatseye
Views: 230
Words: 271
Pages: 2
Category: Science and Technology
Date Submitted: 01/04/2012 04:26 PM
Array declaration and definition One-dimensional Array Two-dimensional Array Multi-dimensional Array Strings
Arrays
An array is a collection of variables of the same type and placed in memory contiguously . The set of data in an array is only given one name and the indices are used to differentiate each of the data. The array is important to avoid redundancy of variable declaration and for easy access of data that are placed contiguously in memory. It is the job of the programmer to ensure that the array is large enough to hold what the program will put in them.
One-Dimensional Array
Syntax:
array_name[array_size]; array_name[array_size] = {optional initialization data};
Index
0
1
2
3
4
One-Dimensional Array
Example:
int num[5];
Note:
Min Index =0 Index 0 1 2 3 4 Max Index = size -1
Important:
Data Address
num[0] num[1] num[2] … &num[0], &num[1] &num[2] … or &num, or num
The name of the array is the starting address of the array.
One-Dimensional Array
Example:
int num[5] = { 2, 4, 3, 10, 20};
Index 0 2 1 4 2 3 3 10 4 20
Data Address
num[0]
num[1]
num[2]
…
&num[0], &num[1] &num[2] … or &num, or num
One-Dimensional Array
Total number of bytes:
size_of_data_type * size_of_array
Example:
int x[10]; # of bytes = 2*10 = 20
char y[20]; # of bytes = 1*20 = 20
One-Dimensional Array
Sample Program : Compute Sum of Array Inputs
/* Filename: SumArray.c Program Description: Ask 5 integers and compute the sum. */ #include main() { int num[5]; int x, sum=0; for(x=0; x