Array, same type के elements की एक series है जो contiguous memory locations में store की जाती है।
Array के indices का use करके elements को randomly access किया जा सकता है। इनका उपयोग किसी विशेष प्रकार के primitive data types जैसे int, float, double, char आदि के collection को store करने के लिए किया जा सकता है। C/C++ में एक Array derived data types जैसे structure, pointers इत्यादि को भी store कर सकती है।
For example :
Array1 नामक variable int type के 5 integer type value वाले एक array create किया जा सकता है
Array1
10 | 20 | 30 | 40 | 50 |
- यह single variable द्वारा referred same data types के variables का एक group है।
- इसके elements को contiguous memory locations में store किया जाता है।
- इसे declare करते समय array के size को mentioned किया जाना चाहिए।
- Array elements index को हमेशा शून्य (0) से count किया जाता है।
- Array मेंelements की positions का use करके Array elements तक पहुँचा जा सकता है।
- Array में one या more dimensions हो सकते हैं।
Types of Array :
- Single dimensional Array.
- Multi dimensional Array.
Advantage :
- Code optimization :
हम data को efficiently retrieve या sort कर सकते हैं
- Random access :
हम index location पर located कोई भी data प्राप्त कर सकते हैं।
Disadvantages :
- Limited in size : Array में element के केवल limited size को store कर सकते हैं। यह runtime पर अपना size grow नहीं कर सकता है।
-
Elements का insertion और deletion costly हो सकता है क्योंकि new memory allocation के अनुसार elements को Re-arrange करने की आवश्यकता होती है।
Array declaration in C/C++ :
There may be 5 type of array declaration in C/C++.
1. Array declaration by specifying the size :
#include <iostream>
using
namespace
std;
int
main()
{
// array declaration by specifying size
int
arr1[10];
// With recent C/C++ versions, we can also
// declare an array of user specified size
int
n = 10;
int
arr2[n];
return
0;
}
// Array declaration by initializing elements
#include <iostream>
using
namespace
std;
int
main()
{
int
arr[] = { 10, 20, 30, 40};
return
0;
// Compiler creates an array of size 4.
// above is same as "int arr[4] = {10, 20, 30, 40}"
}
3. Array declaration by specifying the size and initializing element :
#include <iostream>
using
namespace
std;
int
main()
{
// Array declaration by specifying size and initializing
// elements
int
arr[6] = { 10, 20, 30, 40 };
// Compiler creates an array of size 6, initializes first
// 4 elements as specified by user and rest two elements as
// 0. above is same as "int arr[] = {10, 20, 30, 40, 0, 0}"
return
0;
}
Accessing Array element in one dimension:
हम index द्वारा किसी Array के element को access कर सकते हैं। मान लीजिए कि हमने एक Array n declare की है। first element n [0] है, दूसरा element n [1] है, और इसी तरह आगे के elements है।
Syntax :
<Array_name>[index];
Array contiguous memory allocate करता है। इस प्रकार, यदि integer की एक array के first element का Address 223698688 है, तो second element का address 223698692 (223698688+4( integer की size 4 byte होती है)) और third element 223698696 होगा, और इसी तरह आगे के elements भी address store करते है । इसका मतलब यह है कि एक array के सभी elements की memory एक साथ allocate की जाती हैं और continuous होती हैं।