well , पिछले topic में आपने Array के बारे में पढ़ा और समझा। Array के though हम multiple values को single variable में store करते थे। एक तरह से कई values का group बनाकर एक single variable में assign करते थे। इसके अलावा C में struct (structure) functionality भी provide की है।


struct (Structures) कई variables का एक group है , means हम अलग अलग variables की grouping करके एक single variable में store करते हैं जिसे struct कहते हैं। struct में present हर एक variable की member कहते हैं।

C create a structure

structure create करने के लिए struct keyword का use किया जाता है , और सभी variables को curly braces {} के अंदर semicolon separated करके लिखा जाता है। इसमें आप different - different types के variables को भी group कर सकते हैं।

struct mystruct {   // Structure declaration
  int num;           
  char letter;       
}; // End the structure with a semicolon user;

structure को define करने के बाद आप , इसमें define किये गए member के type के according values assign कर सकते हैं लेकिन structure use करने के लिए आपको इसका variable बनाना पड़ता है।
For Example :

struct mystruct m1;
m1.num = 90;
m1.letter = "D";

members को value assign करने के बाद आप ठीक इसी तरह से access भी कर सकते हैं।

// to print use struct variable name like : 
printf("%i", m1.num);

C structure Example

CopyFullscreenClose FullscreenRun
#include <stdio.h>
// define a structure : student.
struct result{
  float marks;
  char grade;
} ;
  
int main() {
  // now make variable
  struct result r1;
  
  // assign value to the structure member.
  r1.marks = 90.5;
  r1.grade = 'A';
  
  // access structure members.
  printf("Marks : %f\n", r1.marks);
  printf("Grade : %c", r1.grade);
  return 0;
}
Output
Marks : 90.500000
Grade : A

आप चाहे तो Array variable की तरह ही struct member variable में भी एक साथ values को assign कर सकते हैं। और need के according values को update कर सकते हैं।

struct result r1 = {90, 'A'};

हालाँकि आप चाहे तो एक structure के कितने ही variables बना सकते हैं।

CopyFullscreenClose FullscreenRun
#include <stdio.h>
// define a structure : student.
struct result{
  float marks;
  char grade;
} ;
  
int main() {
  // now make variable
  struct result r1 = {90, 'A'};
  struct result r2 = {50, 'C'};
  struct result r3 = {40, 'D'};

  // access structure members.
  printf("r1 Marks : %f , Grade %c\n", r1.marks, r1.grade);
  printf("r2 Marks : %f , Grade %c\n", r2.marks, r2.grade);
  printf("r3 Marks : %f , Grade %c\n", r3.marks, r3.grade);
  return 0;
}
Output
r1 Marks : 90.000000 , Grade A
r2 Marks : 50.000000 , Grade C
r3 Marks : 40.000000 , Grade D

Define string in structure

structure में string variables को आप define तो normally जैसे करते हैं बैसे कर सकते हो , लेकिन string value को आप directly assign नहीं कर सकते हैं। और directly assign करते हैं कुछ इस तरह से error आती है।

CopyFullscreenClose FullscreenRun
#include <stdio.h>
struct result{
  char name[50]; // to store name.
  float marks;
  char grade;
} ;
  
int main() {
  // now make variable
  struct result r1;
  r1.name = "Rahul";
  return 0;
}
Output
test.c: In function 'main':
test.c:12:11: error: assignment to expression with array type
12 |   r1.name = "Rahul";
   |           ^

structure member variable में string value assign करने के लिए strcpy() function का use किया जाता है।

CopyFullscreenClose FullscreenRun
#include <stdio.h>
struct result{
  char name[50]; // to store name.
  float marks;
  char grade;
} ;
  
int main() {
  // now make variable
  struct result r1;
  strcpy(r1.name, "Rahul Verma");
  r1.marks = 90;
  r1.grade = 'A';
  printf("Name : %s\n", r1.name);
  printf("Marks : %i\n", r1.marks);
  printf("Grade : %c", r1.grade);
  return 0;
}
Output
Name : Rahul Verma
Marks : 90
Grade : A

आपको C language में structure अच्छे से समझ आ गया होगा।

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