Artikel Terbaru

Structures

Introduction to C Structures


Introduction to C Structures 
आप data types के बारे में पहले पढ़ चुके है। Predefined data types (int, char, float आदि) की तरह C user defined data types भी प्रोवाइड करती है। ऐसा ही एक user defined data type structure होता है। 

Structure में आप दूसरे predefined data types को create कर सकते है और एक record तैयार कर सकते है। जैसे की आप किसी व्यक्ति के बारे में उसका नाम,पता और उम्र store करवाना चाहते है तो उस व्यक्ति के नाम से एक structure create कर सकते है और उसमे ये तीन variables create कर सकते है।



ऐसा करने से सारी information एक ही जगह पर होगी और एक ही नाम के द्वारा access की जा सकती है।



Structure किसी array की तरह ही होता है। इन में difference इतना होता है की array में आप एक ही type के data को store कर सकते है लेकिन structure के द्वारा different types के data को store किया जा सकता है।


जब एक बार आप कोई structure create करते है तो ये एक data type बन जाता है। अब आप इस data type के कितने भी variables create कर सकते है। और आप इस data type का array भी create कर सकते है।



लेकिन जब आप इस तरह के variable की value initialize करंगे तो आपको उस structure में define किये गए सभी variables की value initialize करनी होगी।



Structure को main method से पहले ही आप define कर सकते है।      

Defining a Structure 

Structure के साथ काम करना बहुत ही आसान होता है। जैसा की मैने आपको पहले बताया ये किसी array की तरह ही होता है। आइये अब देखते है की structure को कैसे define किया जाता है और कैसे use किया जाता है। 


Structure को define करने के लिए struct keyword use किया जाता है। इस keyword के बाद structure का unique नाम दिया जाता है। इसके बाद curly braces में variables create किये जाते है और ending curly bracket के बाद semicolon लगाया जाता है। 

struct struct_Name
{

    //Statement 1;

};
      
मान लीजिये आप किसी tShirt का record store करने के लिए एक structure बना रहे है तो उसे इस प्रकार define कर सकते है।  


struct tShirt
{
    int price;    
};

यँहा पर tShirt नाम से एक structure create किया गया है। इस structure में price नाम से एक variable create किया गया है। आप एक से ज्यादा variables भी create कर सकते है। आप इन variables को structure के अंदर initialize नहीं कर सकते है।



क्योंकि पहले struct (tShirt) type का variable create किया जायेगा फिर उस variable के माध्यम से हर record के लिए अलग से इन variables को initialize किया जाता है। इन variables को structure members कहा जाता है। जैसा की मैने आपको पहले बताया था उसी प्रकार ending curly braces के बाद semicolon लगाया गया है।



Creating Structure Variables 

Structure variables आप 2 तरह से create कर सकते है। 
  • With structure definition 
  • Without structure definition 

With Structure Definition 

जब आप structure definition के साथ ही उस type के variables create करते है तो ending semicolon से पहले आप variables को comma से separate करके लिख देते है। 


struct tShirt
{
   int price;
}t1,t2;
   

Without Structure Definition 

जब आप structure definition के बिना variables create करते है तो struct keyword use करते है। Struct keyword के बाद structure का नाम लिखा जाता है। और इसके बाद comma से separate करके जितने चाहो उतने variables लिख सकते है।  

struct tShirt t1, t2, t3;

Accessing Structure Members 

Structure members को आप 2 वजह से access करते है। या तो आप members को values assign करवाने के लिए या फिर उनकी values को output के रूप में print करवाने के लिए आप structure members को access करते है। जब भी आप किसी भी structure member को access करते है तो ऐसा आप (.) dot operator द्वारा करते है। 

मान लीजिये आप tShirt structure के variables को values assign करवाना चाहते है तो आप ये इस प्रकार कर सकते है। 

t1.price=1000;  
    
यदि आप tShirt structure के variables को output के रूप में print करवाना चाहते है तो ऐसा आप इस प्रकार कर सकते है। 

printf("%d",t1.price);


Example 

#include <stdio.h>

struct tShirt
{
    int price;
};

int main()
{
      struct tShirt t1;

      t1.price=1000;

      printf("Price of tShirt is : %d",t1.price);

      return 0;
Price of tShirt is : 1000 

Structure as Function Argument

एक structure को आप किसी function में argument के रूप में भी pass कर सकते है। इसके लिए आपको किसी प्रकार के special operator की आवश्यकता नहीं होती है। जिस प्रकार आप normal variables को function arguments के रूप में pass करते है उसी प्रकार आप structure object को भी function में pass करते है। 

लेकिन आपको function declaration और definition में parameter को struct keyword के साथ define करना होगा।



return-type function-name(struct-keyword struct-name obj-name)

{

    //Function code here...

}


#include<stdio.h>



struct Person

{

    int Id;

    int Phone;

};



void printPerson(struct Person p1);



int main()

{

    struct Person p1;



    p1.Id = 101;

    p1.Phone = 237434839;



    printPerson(p1);



    return 0;

}



void printPerson(struct Person p1)

{

    printf("Name is : %d\n",p1.Id);

    printf("Phone number is %d\n",p1.Phone);

}


Id : 101

Phone : 237434839

Pointer to Structure

Structure variables के pointers भी create किये जा सकते है। ये उसी तरह create किये जाते है जैसे की आप किसी normal variable के pointers create करते है। 


struct-keyword struct-name *struct-pointer-variable;  //Creating pointer of struct



pointer-variable = &struct-variable;    //Assigning address of struct variable to pointer


#include<stdio.h>



struct Employee

{

    int Id;

    int Phone;

};



int main()

{

   struct Employee e1;

 

   struct Employee *emp;



   emp = &e1;



   emp->Id=101;

   emp->Phone = 237434839;



   printf("Employee Id : %d\n",emp->Id);

   printf("Employee Phone : %d\n",emp->Phone);



   return 0;

}


Employee Id : 101

Employee Phone : 2374334839

BitFields 

C language आपको memory को सही तरीके से utilize करने की capability provide करती है। यदि आप structure के अंदर ऐसे variables create कर रहे है जो पूरी memory को utilize नहीं करते है तो इस situation में आप उन variables की size define कर सकते है और बता सकते है की उस variable के लिए कितनी memory assign की जानी चाहिए।



आपको एक बात ध्यान रखनी चाहिए की ये आप सिर्फ structure और union members के साथ ही कर सकते है। ऐसा किसी normal variable के साथ नहीं किया जा सकता है।



इसका मुख्य उद्देश्य memory को सही utilization है। जब आपको पता हो की किसी struct member की value निश्चित size से अधिक नहीं होगी तो आप ऐसा कर सकते है।



Variable की size आप bits में define करते है। यही reason होता है की ऐसे variables को bitfields कहा जाता है।



struct struct-name

{

    type variable-name : size;

};


#include<stdio.h>



struct Person

{

    int age : 5;

};



int main()

{

   struct Person p1;

   p1.age = 5;



   printf("Age is : %d",p1.age);



   return 0;



ऊपर दिए गए उदाहरण में यदि आप age variable की value 5 bit से अधिक input करते है तो value minus में show होगी।



Age is 5

Post a Comment

0 Comments