Arrays in C


Why arrays?


  • It is used to represent multiple data items of same datatype by using only single name.eg.If you wanted to store marks of 30 students,you would have to use 30 variables,but by arrays you just use 1 variable.
  • Can be used to implement data structures like linked list,stack,queue,graphs,etc


What is an array?
It is a data structure which stores element of same datatype.

  • Syntax

1.  Array declaration
datatype array_var_name[number of elements it will store i.e size];
int arr[3];
2.  Array initialization
int arr[3]={1,2,3};
Specifying array size is optional.
int arr[]={1,2,3};
Also,you can assign elements to an array one by one.
arr[0]=1;
and so on.

Pictorial representation
     arr[0]          arr[1]        arr[2]
 __________________________________________ 
|       1      |       2       |     3     | 
|______________|_______________|___________| 
  0xbfcb8704     0xbfcb8708      0xbfcb870c

Note: Array elements are stored in contiguous memory locations.As each element of our array arr is an integer and an integer requires 4 bytes , so addresses above have difference of 4 bytes.

  •  Accessing array elements

Any ith element in an array can be accessed by
array_name[i];    where i<n (n=size of array)
Lets write a program to demonstrate how to accept marks of 5 students,store them in an array and print them.
#include <stdio.h>

int main(void) 
{
        int marks[5],i;                     //array declaration
        
        for(i=0;i<5;i++)                   
            scanf("%d ",&marks[i]);        //input marks of each student and store them in an array
        for(i=0;i<5;i++)
            printf("%d ",marks[i]);       //print marks of each student
        
        return 0;
}
Without using array we would have required a variable to store the marks of each student and then display it.(total=5 variables).So it wouldn’t be flexible to declare each variable,input it from user and print them.Imagine if there were 100 students.


  • Base address of an array

Array name stores the base address of an array.It is a constant pointer to the 0th element of an array.This can be shown in diagram below:
               arr[0]          arr[1]        arr[2]
          __________________________________________ 
arr------>|       1      |       2       |     3    | 
          |______________|_______________|__________| 
            0xbfcb8704     0xbfcb8708     0xbfcb870c
As it is a constant pointer,we cannot modify its value i.e we can't change the address it is holding i.e it can't point to any other variable.
If you are not comfortable with pointers see pointer basics.
So for accessing ith element of an array you can also use (arr+i)
for(i=0;i<3;i++)
    scanf("%d",arr+i);        //accept array elements and store it in an array arr
arr+i is itself the address of ith element of arr,So no need to use & operator.
for(i=0;i<3;i++)
    printf("%d",*(arr+i));
* operator is used to retrieve the value at memory location arr+i.


More


Pass array to function

Function returning an array

No comments:

Post a Comment