Professional Documents
Culture Documents
Arrays
An array is a collection of similar data items, that are stored under a
common name.
A value in an array is identified by index or subscript enclosed in square
brackets with array number.
For example q[n] represents the array q of n elements.
The array index value starts from 0. So q[0] refers to the first element of the
array.
Arrays can be classified into
One-Dimensional
Two-Dimensional
Multi-Dimensional
Characteristics of array
Instead of declaring n variables for n values, only one variable is
enough using array
Ex: int a[3];
All the elements of an array share the same name
Element number in an array plays major role for accessing each
element
Ex: int a[3]={1,2,3};
a[0]=1 a[1]=2 a[2]=3
Any particular element of an array can be modified separately
without disturbing other element
Ex: int a[3]={1,2,3};
for replace 2 by 10 in above statement,
write statement a[1]=10;
Any array element a[] can be assigned to another ordinary
variable or array variable of its type
Ex: b=a[2];
a[1]=a[2];
One Dimensional Arrays
The collection of data items can be stored under one variable name using
only one subscript, such an variable is called one-dimensional array.
Declaration of Array:
float height[7];
One Dimensional Arrays
In memory seven storage locations are reserved for the
variable height as shown below:
height
10.5 height[0
]
height[1
]
height[2
]
height[3
]
height[4
] into an element in the array simply by
A value can be stored
specifying the statement
height[5
]
height[0] = 10.5;
height[6
the value 10.5 is stored as the first element of the array height.
]
One Dimensional Arrays
Initializing the Arrays:
The general form is
data_type array_name[size] = {list of values separated by comma};
Example:
int num[4] = {1, 2, 3, 4};
The above statement will declare an integer array of size 4 and will assign value
as num[0] = 1, num[1] = 2, num[2] = 3, num[3] = 4.
Declaration of Array:
The general form of declaration is
data_type variable_name[row_size][column_size];
Example:
int marks[4][3];
Function Purpose
variable_name = strlen(string);
strlwr(string);
strlwr(str1);
strupr(string);
strupr(str1);
strrev(string);
Example1:
strrev(str1);
Example1:
char str1[10] = Hello ;
char str2[10] = Hai;
strncpy(str1,str2,2);
In the above statement the content of first 2 characters of
str2 is overwritten to the str1.
Therefore, str1=Hallo
strncpy,strncmp,strncat
Strncmp
Example1:
char str1[10] = Hello;
char str2[10] = Heal;
strncmp(str1,str2,2);
In the above statement the first 2 characters of str1 and
str2 are compared
strncpy,strncmp,strncat
Strncat
Example1:
char str1[10] = Hello;
char str2[10] = Heal;
strncat(str1,str2,2);
In the above statement the first 2 characters of str2
appended to str1
Therefore, s1=HelloHe