C New Unit-4
C New Unit-4
2.1 Introduction:
Memory representation:
The following diagram represents an integer array that has 12 elements. The index of the
array starts with 0, so the array having 12 elements has indexes from 0 to 11.
We can access any array element using array name and subscript/index written
inside pair of square brackets [].
For Example:
◦ Suppose we have an integer array of length 5 whose name is marks.
int marks[5] = {5,2,9,1,1}; Now we can access elements of array marks
using subscript followed by array name.
◦ marks[0] = First element of array marks = 5
◦ marks[1] = Second element of array marks = 2
◦ marks[2] = Third element of array marks = 9
◦ marks[3] = Fourth element of array marks = 1
◦ marks[4] = Last element of array marks = 1
◦ Remember array indexing starts from 0. Nth element in array is at index N-1.
2.4 Storing Values in Array
you can also initialize the array during declaration like this:
int arr[5] = {1, 2, 3, 4 ,5};OR (both are same)
int arr[] = {1, 2, 3, 4, 5};Un-initialized array always contain garbage values.
Using scanf:
◦ For(i=0;i<n;i++)
Scanf(“%d”,&a[i]);
Enter 5 integers:
1
-3
34
0
3
Displaying integers:
1
-3
34
0
3
Things you can and can’t do
One dimensional array is an array that has only one subscript specification that is
needed to specify a particular element of an array.
A one-dimensional array is a structured collection of components (often called array
elements) that can be accessed individually by specifying the position of a
component with a single index value.
Syntax:
data-type arr_name[array_size];
Example: int a[5]
Initialization
After an array is declared it must be initialized. Otherwise, it will
contain garbage value (any random value). An array can be initialized at
either compile time or at runtime.
Output:
2 3 4
Runtime initialization
An array can also be initialized at runtime using scanf() function. This approach is usually
used for initializing large arrays, or to initialize arrays with user specified values. Example,
#include<stdio.h>
void main()
{
int arr[4];
int i, j;
printf("Enter array element");
for(i = 0; i < 4; i++)
{
scanf("%d", &arr[i]); //Run time array initialization
}
for(j = 0; j < 4; j++)
{
printf("%d\n", arr[j]);
}
}
Rules for Declaring One Dimensional Array
An array variable must be declared before being used in a program.
The declaration must have a data type(int, float, char, double, etc.), variable
name, and subscript.
The subscript represents the size of the array. If the size is declared as 10,
programmers can store 10 elements.
An array index always starts from 0. For example, if an array variable is
declared as s[10], then it ranges from 0 to 9.
Each array element stored in a separate memory location.
Points to Remember About Array in C:
An array is a derived data type in C that is constructed from the fundamental
data type of C language.
An array is a collection of similar types of values in a single variable.
In implementation when we required the ‘n’ number of the variable of a
similar data type then we need to go for the array.
When we are working with an array always memory will be created in the
contiguous memory location, so randomly we can access the data.
In an array, all elements will share the same name with a unique
identification value called index.
Always array index will start from 0 and ends with size – 1.
When we are working with an array compile-time memory management will
occur i.e. static memory allocation.
Always size of the array must be an unsigned integer value which should be
greater than zero.
Example Programs:
Two dimensional arrays are most common type of multi dimensional array. A two
dimensional array in C language is represented in the form 2D matrix having rows
and columns.
◦ A two dimensional matrix is an array of one dimensional arrays.
◦ Two dimensional array requires two subscript variables or indexes.
◦ One subscript represents the row index while other represent column index of
an element in matrix.
◦ Elements of a two dimensional array are stored in contiguous memory
location. First all elements of first row are store then all elements of second
row and so on.
Example Programs:
Actually, you do not place the null character at the end of a string constant.
The C compiler automatically places the '\0' at the end of the string when it initializes
the array.
Let us try to print the above mentioned string −
#include <stdio.h>
void main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("Greeting message: %s\n", greeting );
getch();
}
C supports a wide range of functions that manipulate null-terminated strings −
Sr.No. Function & Purpose
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
The following example uses some of the above-mentioned functions −
#include <stdio.h>
#include <string.h>
void main ()
{
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12]; int len ; /* copy str1 into str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );
/* concatenates str1 and str2 */
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 );
/* total lenghth of str1 after concatenation */
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
}
1. Define array?
2. Discuss how to declare an array with example.
3. Explain about array initialization.
4. Explain how to access elements of the Array.
5. Explain about various types of arrays with example.
6. List out some character handling functions.
7. Define String and list out some strings functions.