Sunday, March 16.

Header Ads

Colorful+Black+Friday+Discount+Leaderboard+Ad

Substraction One Pointer to another Pointer

Muscle+Inc.+%25282%2529

Substraction%20One%20Pointer%20to%20another%20Pointer

Substraction One Pointer to another Pointer

किसी Array के दो Elements का Address यदि दो अलग – अलग Pointer Variables में Stored हो तो हम इन्हें आपस में घटा सकते हैं | एक Pointer में से दूसरे Pointer को घटाने पर प्राप्त होने वाला मान प्रथम Element से दूसरे Element के बीच की दुरी Bytes में बताता है | इसे समझने के लिए निम्न उदाहरण देखिये :


#include<stdio.h>
main()
{
	int j[4],*k ,*l;
	k= &j[1];
	l= &j[3];
	
	printf("\n Address of j[1] is %u ",k);
	printf("\n Address of j[3] is %u ",k);
	printf("\n j[3]-j[1] = ",j-i);
	getch();
}

Output

 Address of j[1] is 65520
 Address of j[3] is 65524
 j[3]-j[1] = 2

इस उदाहरण में हम देखते हैं कि j[i] का Address 65520 j[3] का Address 65524 है | j[3] – j[1] करने पर 65524 – 65520 होना चाहिए | लेकिन ऐसा नहीं होता , और इसका मान 2 प्राप्त होता है | ऐसा इसलिए होता है , क्योंकि किसी Pointer में से जब उसी Array के किसी अन्य Element के Pointer को घटाया जाता है , तब प्राप्त होने वाला मान Address की आपस की गणना का मान नहीं होता है , बल्कि ये मान उन दोनों Address के बीच की दुरी बताता है कि दूसरा Element प्रथम Element से कितना दूर या कितनी Byte दूर स्थित है | यहां j[3] , j[1] से दो Byte की दुरी पर स्थित है | क्योंकि ये int प्रकार का Array है और int Memory में दो Byte की Space लेता है |  


 

No comments

Post Top Ad

Post Bottom Ad