← Back to C Course | Chapter 8: Structures & Unions | Lesson 4 of 7

C के Structure और Pointers

एक structure pointer एक structure का address रखता है, यह बताने वाले एक note की तरह कि एक form कहाँ filed है। Arrow operator इसके अंदर पहुँचता है।
Syntax
c
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?

c
// 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;
}

Arrow Operator (->)

ptr->name exactly (*ptr).name के बराबर है, लेकिन कहीं ज़्यादा readable है -- practice में लगभग सारा real-world C code dot-and-parentheses form की बजाय arrow operator इस्तेमाल करता है।

उदाहरण: The Arrow Operator (->)

c
// 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;
}

Dot Notation से Dereferencing

*ptr के आस-पास parentheses ज़रूरी हैं क्योंकि dot operator (.) dereference operator (*) से tighter bind करता है -- बिना parentheses के *ptr.member लिखना गलती से ptr.member को dereference करने की कोशिश करेगा।

उदाहरण: Dereferencing with Dot Notation

c
// 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;
}

Structures को Dynamically Allocate करना

यह exactly वह तरीका है जिससे आप C में linked lists, trees, और दूसरे dynamic data structures बनाते हैं, क्योंकि हर नए node की memory तब तक exist नहीं करती जब तक आप runtime पर malloc(sizeof(struct Node)) से explicitly इसे request न करें।

उदाहरण: Dynamically Allocating Structures

c
// 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;
}

Structures के अंदर Pointers

एक Node *next member वाला एक struct Node linked lists का fundamental building block है -- हर node अगले वाले का address store करता है, आपको एक fixed array size के बिना arbitrary संख्या में elements chain करने देते हुए।

उदाहरण: Pointers inside Structures

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक struct के pointer पर -> की बजाय . इस्तेमाल करना।
  2. memory allocate करने से पहले एक struct pointer इस्तेमाल करना।
  3. एक dynamically allocated struct को free करना भूल जाना।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.