Wednesday, 7 June 2023

Arrays and Pointers in C

What are Arrays and Pointers?

The variables that we have used till now are efficient of set aside only one worth at a time. Think about a position when we want to store and show the age of 100 employees.  There are three type of following:

arrays, pointers, c language, c program
Pointers and Arrays in C language image-1

1.       Declare 100 unlike variables to store the age of employees.

2.       Assign a value to each variable.

3.       Display the value of each variable.

In spite of the fact that we can perform our task by the above three steps but just visualizes how difficult it would be to pick up so many variables in the program and the program would begin to be very lengthy. The idea of arrays is useful in these types of position where we can group alike kind of data items.

An array is an assembly of similar type of data thing and each data item is called an component of the array. The elements of array part the same variable name but each element has a unlike lead number known as subscript.

For the above sample we can take an array variable age[100] of type int. The size of this array variable is 100 so it is ability of storing 100 integer values. The separate elements of this array are –

Age [0], age[1], age[2], age[3], ………………………………………………………..age[98], age[99]

Arrays can be one dimensional or multidimensional. The number of subscripts decide the dimension of array. A one-dimensional array has one subscript, two dimensional array has two subscripts and so on.

1.1   One Dimensional Array

 

1.1.1          Declaration of 1-D array

Like other easy variables, arrays should also be declared previous to they are used in the program.  The following syntax declaration of an array is –

Data_type array_name[size];

Here array_name indicate the name of the array and it can be any reasonable C identifier, data_type is the data type of the elements of array. The size of the array defines the number of elements that can be set aside in the array.

Some examples of array declarations –

int age[100];

float sal[5];

 char grade[20];

1.1.2          Accessing 1-D Array Elements

Identify the array name followed by subscript in square bracket can entrance the elements of an array. In C, the array start subscripts from 0.

1.1.3          processing 1-D Arrays

For take care of arrays we generally use a for loop and the variable is used at the site of subscript. The beginning value of loop variable is taken 0 since array subscripts from zero. The loop variable is enlarging by 1 each time so that we can entry and processes the next element in the array.


Computer stuff kit tricks of Topics 51.