An array is a collection of data items of same type stored in adjacent memory locations. Data items in an array are called array elements. All the elements of an array are of same type. So an integer array can store only integer values and not other types.
The individual values are identified by a number known as array index. If n is the size of array then indexing starts from 0 and last element has index n-1.
int d[20] = {0};
Following are some invalid ways of declaring arrays:
1. void main()
{
int b=7;
int a[b];
}
Size must be constant value.
2. void main()
{
const int b = 7;
int a[b];
}
Here b is constant but compiler will no accept it.
3. void main()
{
int a[];
}
The individual values are identified by a number known as array index. If n is the size of array then indexing starts from 0 and last element has index n-1.
One Dimensional Arrays
Declaring an Array
The syntax of declaring an array is:
data_type arrayname[maxsize];
Here, data_type tells the compiler about data item type. arrayname is a valid identifier used to access elements of the array. maxsize is the size of array.
For example, int colors[11];
Initialization of Array
The array elements can be initialized like
data_type arrayname[size] = { value1, value2,...,valuen };
For example,
int colors[11] = {11, 32, 4, 6};
Value of each element is
colors[0]= 11
colors[1] = 32
colors[2] = 4
colors[3] = 6
colors[4] = 0
colors[5] = 0
colors[6] = 0
colors[7] = 0
colors[8] = 0
colors[9] = 0
colors[10] = 0
colors[11] = 0
Rest of the elements whose values ar not initialized will be set to zero.
Input and Output Values
If we want to input elements of array we use,
scanf("%d", &colors[0]);
To input all elements we use a for loop.
for(int i = 0;i < n;i++)
{
printf("Enter value of colors[%d]", a);
scanf("%d", &colors[i]);
}
To display elements of array we use
for(int i = 0; i < n; i++)
{
printf("Value of colors[%d] is %d", a, colors[a]);
}
The amount of space needed to store an array is directly related to the typeof array and its size.
totalbytes = sizeof(datatype) * size of array
Following are some valid definitions of arrays:
float marks[5];
- marks is an array of type float and size 5. All elements will have garbage values after execution of this statement.
float marks[5]={3.5,6.74};
- marks is an array of type float and size 5. First two elements are 3.5 and 6.74. Rest of the elements are set to zero.
float marks[]={3.5,6.7,8.934};
- marks is an element of float type of three elements initialized with given values in order.
char name[3];
- name is an array of size 3 and type char.
char name[30]={"CProgramming"};
- name is an array of type char and size 30. First 12 cells are used to store string "CProgramming" and 13th cell stores null value.
char name[]={'A','B','v','4'};
- name is an array of 4 elements and type char. All its elements are initialized to given values in order.
int d[20] = {0};
Following are some invalid ways of declaring arrays:
1. void main()
{
int b=7;
int a[b];
}
Size must be constant value.
2. void main()
{
const int b = 7;
int a[b];
}
Here b is constant but compiler will no accept it.
3. void main()
{
int a[];
}
A constant value must appear in brackets.
Two Dimensional Arrays
Two-dimensional arrays are declared in same way as one-dimensional arrays except that a separate pair of square brackets is required.
The syntax for declaring a two-dimensional array is
data_type arrayname[row_size][col_size];
For example
int info[6][5];
Initialization of 2-D Array
Following is an example
#include<stdio.h>
{
int a[3][2] = { {2,4}, {4,1}, {5,6}};
}
Memory Representation
The elements of a two-dimensional array are stored row-wise, that is, in allocated memory, the elements in the first row are stored first, the the elements in the second row and so on.
String or Char Array
We declare and initialize char array as,
char a[5] = { 'A', 'B', 'C', 'D'};
Last element of a char array is null character.
In this case,
a[0] = A
a[1] = B
a[2] = C
a[3] = D
Input and Output Character Array
Input is taken as,
scanf("%c", &a[0]);
To input all elements we write,
for(int i = 0; i < 5; i++)
{
printf("Enter the value of a[%d]",i);
scanf("%c", &a[i]);
}
To display the elements of character array we write,
for(int i =0; i < 5; i++)
{
printf("Value of a[%d] is %c, i, a[i]);
}
To take input as a string we write,
scanf("%s", a);
Array of Strings
The following statement shows way of initializing the strings,
char name[2][10] = { "Siddhant", "Sonia"};
Passing Arrays to Function
Passing One-Dimensional Array
This is done as
return_type function_name(type []);
Passing Two-Dimensional Array
return_type function_name(type [][], size_t rowsize, size_tcolsize);
For example,
fun(a,2,10);
Common Programming Errors
- Declaring an array without specifying any value as size of array.
- Declaring an array and taking size as a variable. C does not allow variable lengths of array.
- Initializing more values than the specified size.
- Accessing values beyond size limits.
- Declaring array of type void.
0 comments:
Post a Comment