C के Structure और Pointers
In this page:
struct StructName *pointer_name = &variable_name;
pointer_name->member; // arrow operator
(*pointer_name).member; // equivalent
Structure Pointer क्या है?
यह उस तरीके का natural counterpart है जिससे एक structure variable data को सीधे रखता है -- एक structure pointer इसकी जगह यह रखता है कि वह data कहाँ रहता है, जो malloc() से structures को dynamically allocate करना शुरू करते ही essential हो जाता है।
उदाहरण: What is a Structure Pointer?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point { int x; };
// Program execution starts in main()
int main() {
struct Point p = {5};
struct Point *ptr = &p;
// Print formatted text to the screen
printf("%d", ptr->x);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Arrow Operator (->)
ptr->name exactly (*ptr).name के बराबर है, लेकिन कहीं ज़्यादा readable है -- practice में लगभग सारा real-world C code dot-and-parentheses form की बजाय arrow operator इस्तेमाल करता है।
उदाहरण: The Arrow Operator (->)
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Student
struct Student { char name[20]; };
// Program execution starts in main()
int main() {
struct Student s = {"Alice"};
struct Student *ptr = &s;
// Print formatted text to the screen
printf("%s", ptr->name);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dot Notation से Dereferencing
*ptr के आस-पास parentheses ज़रूरी हैं क्योंकि dot operator (.) dereference operator (*) से tighter bind करता है -- बिना parentheses के *ptr.member लिखना गलती से ptr.member को dereference करने की कोशिश करेगा।
उदाहरण: Dereferencing with Dot Notation
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point { int x; };
// Program execution starts in main()
int main() {
struct Point p = {5};
struct Point *ptr = &p;
// Print formatted text to the screen
printf("%d", (*ptr).x);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structures को Dynamically Allocate करना
यह exactly वह तरीका है जिससे आप C में linked lists, trees, और दूसरे dynamic data structures बनाते हैं, क्योंकि हर नए node की memory तब तक exist नहीं करती जब तक आप runtime पर malloc(sizeof(struct Node)) से explicitly इसे request न करें।
उदाहरण: Dynamically Allocating Structures
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Define a structure type named Point
struct Point { int x; };
// Program execution starts in main()
int main() {
// Allocate memory on the heap
struct Point *p = malloc(sizeof(struct Point));
p->x = 10;
// Print formatted text to the screen
printf("%d", p->x);
// Release the heap memory so it can be reused
free(p);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structures के अंदर Pointers
एक Node *next member वाला एक struct Node linked lists का fundamental building block है -- हर node अगले वाले का address store करता है, आपको एक fixed array size के बिना arbitrary संख्या में elements chain करने देते हुए।
उदाहरण: Pointers inside Structures
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Node
struct Node {
int value;
struct Node *next;
};
// Program execution starts in main()
int main() {
struct Node second = {2, NULL};
struct Node first = {1, &second};
// Print formatted text to the screen
printf("%d %d", first.value, first.next->value);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक struct के pointer पर
->की बजाय.इस्तेमाल करना। - memory allocate करने से पहले एक struct pointer इस्तेमाल करना।
- एक dynamically allocated struct को
freeकरना भूल जाना।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: