हिंदी माध्यम नोट्स
C99- Feature : array ,function और pointer related modification in c language in hindi
C language मे , array के subscript को integer से initial करते है जिससे array की size fixed हो जाती है |लेकिन C 99 मे हम किसी भी expression को array के subscript को initial करते है |इन variable की value vary हो सकते है | अतः जब इन variable को run time पर declare किया जाता है तब array की size change हो सकता है |
इसका उदाहरण है :
#include<stdio.h>
#include<conio.h>
void main()
{
int a[n];
int n;
printf(“Enter Size of array “);
scanf(“%d”,&n);
printf(“Entyer Elements”);
int(int i=0;i<n;i++)
{
scanf(“%d”,a[i]);
}
int(int i=0;i<n;i++)
{
printf(“Array [%d] = %d”,i,a[i]);
}
getch();
}
इस उदहारण मे , variable ‘n’ को declare किया गया है |और यूजर से ‘n’ की value input करते है |और इसे array के सुन्स्क्रिप्त मे pass करते है |
इसका उदाहरण है :-
Enter Size of array 3
Entyer Elements 23 343 45
Array [0] = 23
Array [1] = 343
Array [2] = 45
और
Enter Size of array 5
Entyer Elements 23 343 45 56 67
Array [0] = 23
Array [1] = 343
Array [2] = 45
Array [4] = 67
2.Type Specification in array declaration
जब हम किसी array को function argument मे pass करते है तब array के dimension मे static keyword का use किया जाता है |static keyword यहा declare करता है की array मे fixed size के element होने चाहिए |
#include<stdio.h>
#include<conio.h>
int avg (int a [ ]);
void main()
{
int a[10];
printf(“Enter Elements”);
int(int i=0;i<10;i++)
{
scanf(“%d”,a[i]);
}
int avg = avg (a[static 5);
printf(“Average of array element = %d”,avg);
getch();
}
int avg (b[static 5)
{
int sum ;
for (int i=0;i<5;i++)
{
sum = sum + b[i];
}
int avg = sum / 5;
return(avg );
}
इस उदाहरण मे , static 5 का मतलब है की array a के सिर्फ 5 elements हो function avg () मे pass होगे |
आउटपुट होगा :
Enter Elements 23 34 45 56 67 56 56 67 78 89
Average of array element = 45
Flexible Array
जब हम किसी structure में array को declare करते है तब C 99 मे , flexible array को declare किया जाता है |
flexible array का मतलब है एसी array जिसकी size define ना हो |flexible array को use कर्ण एके लिए कुछ rules होते है :-
1.flexible array structure की last member होते है |
2.flexible array के dimension सेक्शन मे कोई भी value नहीं होती है |
3.structure flexible array के अलावा दुसरे भी member होने चाहिए |
flexible array से dynamic memory allocation :
जब किसी structure से dynamic memory allocation किया जाता है |तब इसकी size को calculate किया जाता है |जैसे
struct student{
int age;
int class;
int grade;
char name [ ]; // flexible variable
};
इस structure के लिए ,
int age: age एक integer type data है जिसका size 4 bytes है |
int class : class एक integer type data है जिसका size 4 bytes है |
int grade : grade एक integer type data है जिसका size 4 bytes है |
char name [ ] : char name [ ] एक flexible array है जिसका size 0 bytes है |
Total size = 4+4+4+0 = 12 bytes
जब किसी flexible array से sturuture को declare किया जाता है तब इसका syntax है :-
struct student *s1 = malloc(sizeof(*s1)+sizeof(char[strlen(name)]);
यहा पर :
*s1 : ये pointer variable है जिसमे allocate memory के address को point करता है |
sizeof(*s1): ये structure student की size को calculate करता है लेकिन flexible array की size को ‘0’ consider करता है |
sizeof(char[strlen(name)]) : ये structure मे declare flexible array की size को calculate करता है |
अगर flexible array की size 6 है तब sizeof(char[strlen (name)]) की value 6 bytes होगी |
उदाहरण के लिए :
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int create_structure(struct student *stu , int a , int c , char n[ ]);
void print_structure (struct student *stu);
struct student{
int age;
int class;
int size; // this variable used for store size of structure student //
char name [ ]; // flexible variable //
};
void main()
{
struct student *stu1=create_structure(stu1,12,3,parth);
struct student *stu2=create_structure(stu2,13,3,om);
print_structure(stu1);
print_structure(stu2);
printf(“Size of structure student= %u \n”,sizeof(struct student));
getch();
}
int create_structure(struct student *stu , int a , int c , char n[ ])
{
stu=malloc(sizeof (*stu)+sizeof(char)*sizeof (name));
stu.age=a;
stu.class=c;
strcpy(stu.name , n);
stu.size= sizeof(*stu)+sizeof(char)*sizeof (name);
return(stu);
}
void print_structure (struct student *stu)
{
printf(“Age : %d\n
Class : %d \n
Name : %s \n \n”,
stu.age, stu.class, stu.name );
printf(“Size of structure =%d”,stu.size);
}
इस प्रोग्राम मे , तीन function को use किया गया है |
1.main() : इस function मे , यूजर से data लिया गया है जिसे बाकि के दो functions मे pass किया जाता है |लेकिन इस प्रोग्राम मे , हमने direct values को function मे pass किया गया है |
2.create_structure() : इस function का use , structure के लिए memory allocate करने के लिए किया जाता है |इसके अलावा main function मे से pass की गयी student details को pointer variable stu1 मे assign किया जाता है |
3.print_structure : इस function का use , structure के देतिअल्स को print करने के लिए किया जाता है |
आउटपुट होगा :
Age : 12
Class : 3
Name : parth
Size of structure =13
Age : 13
Class : 3
Name : om
Size of structure =10
Size of structure student= 8
Recent Posts
मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi
malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…
कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए
राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…
हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained
hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…
तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second
Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…
चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi
chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…
भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi
first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…