Thursday 5 December 2019

C Tutorial - Array and String with Examples


Array and String

Introduction of Array


Whenever there is need to store a group of data of the same type in the memory, arrays are used. Two terms has been described here Array and String.

Strings in C are represented by the array only therefore it would be convenient to describe stringsseparately in the same block but in the different section.

Arrays are the contiguous memory location used to store similar data type or in other terms we can say Arrays are a data structure which holds multiple variables of the same data type.

Consider the case where a programmer needs to keep track of a number of people within an organization. So far, our initial attempt will be to create a specific variable for each user.

This might look like:

int name1 = 101;
int name2 = 232;
int name3 = 231;

It becomes increasingly more difficult to keep track of this as the number of variables increases.Arrays offer a solution to this problem.

An array is a multi-element box, a bit like a filing cabinet, and uses an indexing system to find each variable stored within it.

In C, indexing starts at zero. Arrays, like other variables in C, must be declared before they can be used.

The replacement of the above example using arrays looks like:

int names[4];
names[0] = 101;
names[1] = 232;
names[2] = 231;
names[3] = 0;

We created an array called names, which has space for four integer variables. You may also see that we stored 0 in the last space of the array.

This is a common technique used by C programmers to signify the end of an array.



Array Declaration

Arrays are defined in the same manner as ordinary variables, except that each array name must be accompanied by the size specification.

The general form of array declaration is:

data_type array_name[size];

data-type specifies the type of array, size is a positive integer number or symbolic constant that indicates the maximum number of elements that can be stored in the array.

Example:
float height[50];

This declaration declares an array named height containing 50 elements of type float.

Array Initialization

The Element of array are initialized in the same way as the ordinary variables.

Example:

int num[6]={2,4,5,35,12,12,67};



When the count has the value 0, the scanf() statement will cause the cvalue to be stored atnum[0].

The process continues until count has the value greater than 5.

Reading data from Array

In the above program we enter the data into an Array. Now to read value from this array , we will again use for Loop.



Two Dimensional Array

Two dimensional array (2-D array) is also called Matrix

General form of 2-D array is:

data_type array_name[row_size][column_size];

Example:
int marks [4][2]

Different ways of Initialization of a 2-Dimensional Array:

int table [2][3]={0,0,0,1,1,1};
initialization done by row.

int table[2][3]={{0,0,0},{1,1,1}};
surrounding the elements of each row by braces.

int table[2][3]={{0,0,0,},
initialization as matrix.



Multi Dimensional Array

Arrays of three or more dimension is called Multi-Dimensional Array.

General form Multi-Dimensional Array:

data_type array_name[s1][s2][s3]......[sn];

Example:
int survey[3][5][12]

Here survey is a 3-dimensionalarray declared to contain 180 integer_type elements.
(3x5x12=180)

Initialization of 4-Dimensional Array:

static int arr [3] [4] [2]={{{2,4}, {7,3}, (3,4}, {5,1}, }, {{3,4}, {3,4}, {3,2}, {4,5}}, {{2,3}, {2,7}, {2,3}, {4,3}}}

In this example, the outer array has three element , each of which is a two dimensional array of four rows, each other of which is a one dimensional array of two elements.


String

Strings in C are represented by arrays of characters. The end of the string is marked with a special character, the null character, which is simply the character with the value 0.

Because C has no built-in facilities for manipulating entire arrays (copying them, comparing them, etc.), it also has very few built-in facilities for manipulating strings.

In fact, C's only truly built-in string-handling is that it allows us to use string constants (also called string literals) in our code.

Whenever we write a string, enclosed in double quotes, C automatically creates an array of characters for us, containing that string, terminated by the \0 character.

For example, we can declare and define an array of characters, and initialize it with a string constant:

char string[] = "Hello, world!";

In this case, we can leave out the dimension of the array, since the compiler can compute it for us based on the size of the initializer.

This is the only case where the compiler sizes a string array for us, however; in other cases, it will be necessary that we decide how big the arrays we use to hold strings.


The last element is filled with a zero value, to signify the end of the character string (in C, there is no string type, so character based arrays are used to hold strings).

A printf statement is then used to print out all elements of the array.



Previous Page                                     Next Page

More Topics: 










100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...