Read in Hindi
Read in English
Going forward with C/C++ programming language tutorial today we will learn about structure(struct). Till now we have learned about many datatypes like int, char, float, double etc. These all datatypes store data in some format. If we want to store names of people, we can store in char array, If we want to store ages of people, we can store in int array but if we want to store length and width of rectangles(storing rectangle) then there are 2 ways. One is make 2 variables and store length and width on them. If we want to store multiple rectangles then we have to make 2 arrays, one for length and another for width. Using struct one rectangle can be stored in one variable and Rectangles can be stored in one array.

Lets see the program which does not use struct to store length and width of a rectangle. Later we will see better version which uses struct.
#include <stdio.h>

int main() {
  int length1 = 12;
  int width1 = 8;

  int length2 = 20;
  int width2 = 11;

  printf("Rectangle1: %d %d\n", length1, width1);
  printf("Rectangle2: %d %d\n", length2, width2);

  scanf("%d", &length1);
}
It is easy to understand program given above. Now we will write same thing with the use of struct.
#include <stdio.h>

struct Rectangle {
  int length;
  int width;
};
int main() {
  struct Rectangle rect1;
  struct Rectangle rect2;
  rect1.length = 12;
  rect1.width = 8;  
  rect2.length = 20;
  rect2.width = 11;

  printf("Rectangle1: %d %d\n", rect1.length, rect1.width);
  printf("Rectangle2: %d %d\n", rect2.length, rect2.width);

  scanf("%d", &(rect1.length));
}

Here we have defined a struct named Rectangle which contains 2 variables width and height. Here Rectangle becomes a datatype like int, char. This Rectangle datatype can store 2 int: height and width. To define new datatype Rectangle see above how we have done it. struct Rectangle { ... };
This have defined a new datatype Rectangle. To use it and make a variable of type Rectangle as shown in main(){...} Here two variable rect1 and rect2 of Rectangle type have been defined. Each of two variable can have length and width. To access length and width of rect1 we use rect1.length and rect1.width.

Using struct we can make any kind of object which can store several variables.
If you liked this blog, tell your friends also because it can useful to them also!!
आज Hindi के इस C/C++ programming language tutorial को आगे बढ़ाते हुए हम structure(struct) के बारे में और जानेंगे. अभी तक हम बहुत से datatype के बारे में पढ़ चुके हैं जैसे कि int, char, float, double etc. ये सभी datatype किसी format में data store करते हैं. जैसे कि अगर हमें लोगों के नाम store करना है तो हम char की array में store कर लेंगे, किसी की age store करना है तो int में store कर लेंगे. परन्तु अगर हमें किसी आयत(rectangle) की length और width store करना है तो एक तरीका ये हैं 2 int variable बनाये और उसमें store करें. उसके बाद अगर दूसरे आयत की length और width store करना है तो 2 नए variable अलग नाम से बनाना पड़ेंगे. struct का use करके अलग अलग नाम से variable बनाने कि दिक्कत दूर की जा सकती है. struct का use करने हर एक आयत का एक variable ही बनाना पड़ेगा

दो आयत की length और width store करने का program struct का use किये बिना नीचे दिया जा रहा है, उसके बाद हम उसका better version struct का use करके भी देखेंगे.
#include <stdio.h>

int main() {
  int length1 = 12;
  int width1 = 8;

  int length2 = 20;
  int width2 = 11;

  printf("Rectangle1: %d %d\n", length1, width1);
  printf("Rectangle2: %d %d\n", length2, width2);

  scanf("%d", &length1);
}
ऊपर दिए गए program को समझाने कि कोई जरूरत नहीं है. अब हम इसी को struct का use करके कैसे लिखेंगे वो देखते हैं.
#include <stdio.h>

struct Rectangle {
  int length;
  int width;
};
int main() {
  struct Rectangle rect1;
  struct Rectangle rect2;
  rect1.length = 12;
  rect1.width = 8;  
  rect2.length = 20;
  rect2.width = 11;

  printf("Rectangle1: %d %d\n", rect1.length, rect1.width);
  printf("Rectangle2: %d %d\n", rect2.length, rect2.width);

  scanf("%d", &(rect1.length));
}

इसे ध्यान से समझने कि जरूरत है. सबसे पहले हमने एक struct define किया है जिसका नाम Rectangle है और उसमे दो variable length और width हैं. यहाँ Rectangle एक datatype बन जायेगा. जिस तरह int एक datatype होता है जो कोई भी संख्या store कर सकता है उसी तरह से यहाँ पर Rectangle एक datatype बन जायेगा जो दो int store कर सकता है जिनके नाम length और width हैं. इसे बनाने का तरीका ध्यान से ऊपर देखिये. struct के बाद वह नाम लिखे जिस नाम से datatype बनाना है. जिस तरह से int नाम का datatype है उसी तरह यहाँ Rectangle नाम का datatype बनाया है. उसके बाद {} में वो सब variable लिखते हैं जो उस datatype में store होंगे. जैसे यहाँ Rectangle datatype में दो variable length और width store होंगे. अंत में ; लगाना न भूले अन्यथा compile करने में error आएगा.
अब Rectangle datatype define हो गया है अब हम इसका use करना देखते हैं. जिस तरह से हम int variable define करते हैं उसी तरह Rectangle variable define करते हैं, परन्तु Rectangle से पहले struct लिखना जरूरी है. यहाँ Rectangle type के दो variable rect1 और rect2 define किये गए हैं. प्रत्येक variable दो value length और width ले सकता है जैसा कि पहले struct datatype बनाते समय define किये थे. अब rect1 की length और width rect1.length, rect1.width से access होती है.

यहाँ rect1 एक Rectangle datatype बन गया है यहाँ यह datatype दो value length और width store कर सकता है. इस तरह से किसी भी object को इस तरह के struct बनाकर store कर सकते हैं.
इसे अपने दोस्तों को बताना न भूले. नीचे दिए गए link की सहायता से इसे facebook, twitter, Buzz पर share अपने दोस्तों से करें.