C Arrays First Part


Hi friends there are thousands of tutorial when in comes to C language, still i feel it is not yet explored in a proper way. Would like to try my hand here, hope i will be able to make you understand a very important concept of  C, Arrays. An array is a data structure used to process multiple elements with the same data type when a number of such elements are known. An array is derived data type in c programming language which can store similar type of data in continuous memory location. Data may be primitive type (int, char, float, double…), address of union, structure, pointer, function or another array.

When to use an Array??

For example, you want to find out the average grades of a class based on the grades of 50 students in the class.

Consider you need to find out the grades of class-3A, class-3B, class-3C sections. Each section is having say 20,30,40 students respectively. Suppose if you consider for 3rd class , 5 number of subjects, then how can you declare the variables to store for each student marks for each subject.

In normal way,

float 3a,3b,3c;

float 3a_1_sub1,3a_1_sub2,… 3a_1_sub5;     // for 1st  student in class-3A

float 3a_2_sub1,3a_2_sub2,… 3a_2_sub5;     // for 2nd student in class-3A

..

is it possible to declare the variables in practical environment???

So, our C designers introduced a concept called Array.

Using an array, you can store grades of 50 students in one entity, say grades, and you can access each entity by using subscript as grades[1], grades[2]. Thus you have to define the array of grades of the float data type and a size of 50.

How to declare an array??

If we need 20 integer variables

int arr_name[20];

like this, what ever the data type available in C we can have array for that. For example,

float arr_floats[30];  double arr_floats[14];  char arr_chars[30];

format of array declaration:

data_type array_name[size];

Now, we will check out a simple program which shows how to declare an array, and how to use in C program.

Example Program

#include <stdio.h>

main()

{

    int a[5];  //A

    int i;

    for(i = 0;i<5;i++)

    {

       a[i]=i;//B

    }

    printarr(a);

}

void printarr(int a[])

{

    int i;

    for(i = 0;i<5;i++)

    {

        printf("value in array %d\n",a[i]);     // C

    }

}

Explanation

  • Statement A defines an array of integers. The array is of the size 5—that means you can store 5 integers.
  • Array elements are referred to using subscript; the lowest subscript is always 0 and the highest subscript is (size –1). If you refer to an array element by using an out-of-range subscript, you will get an error. You can refer to any element as a[0], a[1], a[2], etc.
  • Generally, you can use a for loop for processing an array. For the array, consecutive memory locations are allocated and the size of each element is same.
  • The array name, for example, a, is a pointer constant, and you can pass the array name to the function and manipulate array elements in the function. An array is always processed element by element.
  • When defining the array, the size should be known.

Note: The array subscript has the highest precedence among all operators thus a[1] * a[2] gives the multiplication of array elements at position 1 and position 2.

 

How to access the array elements??

int arr[20];

From the above array declaration, we can conclude that it’s an array of 20 integer variables. Now if we want access 3rd variable

arr[2]=14;

Here, to access the 3rd one you need to use index=2, because the index starts from 0.

Writing into array element:

scanf(“%d”,&arr[2]);  // Here we are using the 3rd element address to store the value in it.

Reading from array:

printf(“%d”,arr[5]);     // this prints the value of 4th element in that array.

To brush up our minds, look into the following points.

Summary:

  • Collection of similar data types is nothing but an array. Array elements are accessed using subscript
  • We call the subscript in general the index, it starts from 0. So, if the size of an array is ‘n’, then array index starts from 0 to n-1.
  • An array is also known as subscripted variable.
  • Before using an array it’s dimension must be declared.
  • Array elements get stored in the contiguous memory locations based on data type.(this point will be it’s self another part of this series, and click here to know how array elements get stored)
Please comment to make it more understandable, that will help others to understand it easily

 

Related posts:

About author

This article was written by admin

Admin has over twenty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles. Working presently as Development Manager in India. A firm Believer in Knowledge grows when it shared.

Comments

Comments (1)
  1. kasyna internetowe says - Posted: October 24, 2012

    In addition to several days routine soon after going back house, I additional another: examined your current web page from the seek out completely new posts. I like your site, every entry I actually study together with bated breath!

Leave your comment

Your email address will not be published. Required fields are marked *