अभी तक आपने को भी examples देखे , उनमे हमें variables को अलग - अलग define करना पड़ता था , लेकिन variables में एक problem थी कि वह सिर्फ single value को store कर सकता है। Let's suppose की हमें integer type के 5 variables use करने हैं तो सभी define करने पड़ेंगे। जिससे code बड़ा हो जयगा और manage करना भी मुश्किल , इस problem को solve करने के लिए C में एक data structure provide किया : Array .


C में array same data types values का एक collection है। means हम एक single array variable में same data type की एक ज्यादा values को store कर सकते हैं।

C define array

C में array को उसके type और size के साथ brackets [] का use करके define किया जाता है। type बताता है कि , array किस type की values को store करेगा और size बताता है कि array में कुल कितने elements होंगे।

data_type array_name[size];

or

data_type array_name[size] = {comma separated elements according to size and type};

हालाँकि अगर आप initialization के time value assign नहीं करना चाहते हैं तो भी कोई बात नहीं , index की help से बाद में assign कर सकते हैं लेकिन array size define करना पड़ेगा।

int num[3];

// later you can assign values according to index.
like : 

num[0] = 10;
num[1] = 20;
num[2] = 30;

Note - C में array define करने के लिए हमें array का type और size define करने की जरूरत होती है। लेकिन कुछ languages जैसे JavaScript और PHP में array define करते समय array type और size define करने की कोई जरूरत नहीं होती है। और उसमे किसी भी type की values store करा सकते हैं।

C array example

int num[3] = {10, 20, 30};

चूंकि array में values एक से ज्यादा हैं , तो हर value की एक position होती है जिसे index कहते हैं। Array में value की indexing 0 से n-1 होती है। यह n कुल elements की सख्यां है। इन्ही index के bases पर array values को insert / update किया जाता है। For Example ऊपर दिए गए example में values की indexing कुछ इस तरह से होगी।

Index 0 1 2
Values10 20 30

ध्यान रहे एक बार array define करने के बाद आप उसके size को change नहीं कर सकते हैं , और न ही दूसरे type की value insert कर सकते हैं।

C access array element

तो जैसे कि आपने अभी पढ़ा कि index के bases पर array values को insert / update किया जाता है तो ऊपर define किये गए array elements को कुछ इस तरह से access करेंगे।

cout << users[0] // Output : 10
cout << users[1] // Output : 20
cout << users[2] // Output : 30

C update array values

इसी तरह index का use करके आप directly new value assign करके array value को update कर सकते हैं , जैसे -

users[0] = 40;
CopyFullscreenClose FullscreenRun
#include <stdio.h>
int main() {
  // define array.
  int num[3] = {10, 20, 30};
  // accessing array elements;
  printf("%d\n",num[0]);
  printf("%d\n",num[1]);
  printf("%d\n",num[2]);
  
  // update array values.
  num[0] = 40;
  // print new value
  printf("%d",num[0]);
  return 0;
}
Output
10
20
30
40

Define string array

String array define करने के लिए भी same process ही है बस , आपको char की जगह char* use करना होगा।

CopyFullscreenClose FullscreenRun
#include <stdio.h>
int main() {
  // define string array.
  char* users[3] = {"Rahul", "Ravi", "Raju"};
  
  // accessing array elements;
  printf("%s\n",users[0]);
  printf("%s\n",users[1]);
  printf("%s\n",users[2]);
    
  // update array values.
  users[0] = "Shyam";
  
  // print new value
  printf("%s",users[0]);
  return 0;
}
Output
Rahul
Ravi
Raju
Shyam

C advantages of array

  • Code Optimization (less code)
  • Random Access
  • Easy to traverse data
  • Easy to manage data
  • Easy to understand.

C disadvantage of array

Fixed size

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers