2013-09-10 14 views
5

में पॉइंटर का उपयोग क्यों किया जाता है, मुझे समझ में आता है कि यह कथन एक हस्ताक्षरित अस्थिर चार को स्मृति पते पर कास्टिंग कर रहा है लेकिन जो मुझे समझ में नहीं आता वह अस्थिर के बाद सूचक है।मुझे समझ में नहीं आ रहा है कि

#define PORTC *(unsigned char volatile *)(0x1003) 
+1

आप प्रोसेसर जहां एक मैं/हे बंदरगाह एक विशिष्ट स्मृति पते में मैप किया गया है पर यह देखने, इसलिए जब आप पढ़ सकते हैं या 'PORTC' बारे में आप बाहरी डिवाइस के साथ जानकारी का आदान-प्रदान कर रहे हैं। –

उत्तर

5

इसे कहते हैं: एक अस्थिर अहस्ताक्षरित चरित्र सूचक के रूप में संख्या 0x1003 इलाज; उस पते पर (बाइट) मान को पढ़ें या लिखें, इसका उपयोग कैसे किया जाता है:

unsigned char c = PORTC; // read 
PORTC = c + 1;    // write 
5

ऐसा नहीं हो रहा है। इसके बजाय, यह मूल्य 0x1003 को पॉइंटर के रूप में व्याख्या कर रहा है, और फिर यह dereferencing है कि सूचक volatile unsigned char प्रकार का मान प्राप्त करने के लिए सूचक है। अनिवार्य रूप से, यह एक निश्चित स्मृति स्थान पर बाइट तक पहुंचने का एक तरीका है। ("volatile" है कि स्मृति स्थान है, जो मानक में कुछ हद तक एक अल्प परिभाषित अवधारणा है करने के लिए एक वास्तविक "पहुंच" लागू करता है।)

0
(TYPE*) POINTER 

एक वाक्य रचना जो सूचक का एक प्रकार है-कलाकारों के रूप में व्याख्या की है है। उदाहरण के लिए,

int m = 4; 
char* p_char = (char*)&m; 

इस प्रकार, p_char चार के लिए सूचक है। अगर हम p_char, *p_char को बाधित करते हैं, तो p_char अंक की स्थिति में द्विआधारी प्रतिनिधित्व char (वर्ण) के प्रतिनिधित्व में परिवर्तित हो जाएगा। इस मान का सटीक परिणाम अपरिभाषित है। मुझे नहीं पता कि यह कितना सटीक मूल्य लौटाएगा, लेकिन यह एक अजीब चरित्र č की तरह कुछ वापस करेगा।

पॉइंटर की गहरी समझ के लिए, मैं जोर देना चाहता हूं कि एक सूचक केवल सी ++ भाषा में प्रदर्शित इकाई तक पहुंचने और कंप्यूटर मेमोरी पर रहने वाली इंटरफेस में से एक है।

#include <iostream> 
using namespace std; 

/* 
* C++ interface to represent entity which resides on computer memory: 
*  ---------            
* 1) object; 
* 2) pointer; 
* 3) reference; 
* 
* C++ interpretation of entity through interface: TYPE 
*  --------------        ----   
* 
* What is the function of TYPE? 
* 1) tell compiler the size of an object of this TYPE needed; (sizeof(int) -> 4 bytes) 
* 2) when the value of object at which it resides is dereferenced, 
*  the binary represented value is transformed to some other 
*  representation value according to the TYPE's interpretation rule; (if int, interpret it as an int) 
* 
* 
*    +----------------+   
*    |  0x02105207 |      
*    |  0x02105206 |      
*    |  0x02105205 |      
*  int n |  0x02105204 |      
*    +----------------+      
*    |  0x02105203 |      
*    |  0x02105202 |      
*    |  0x02105201 |      
* ---->int m |  0x02105200 |      
* |   +----------------+     
* |   |    |      
* |   +----------------+     
* |   ...    ...      
* |   +----------------+     
* ---- int* p |  0x00002298 |      
*    +----------------+     
* 
* if the pointer in figure is declared as: 
* 
*  int* p = &m; 
* 
* the face of 0x00002298 -> 0x02105200 will not be changed until p is 
* assigned to other address value; 
* 
* 
*/ 

class consecutive_two_int 
{ 
public: 
    consecutive_two_int():m(4),n(123){} 
    int m; 
    int n; 
}; 

int main() 
{ 
    int * p; 
    consecutive_two_int obj; 
    p = &(obj.m); 

    for(int i = 0; i < 8; ++i) 
    { 
     // because pointer of char progresses every 1 byte; 
     // increment memory byte by byte through it; 
     cout << *(int*)((char*)p + i) << "\n"; 
    } 

    return 0; 
} 

परिणाम, एक उदाहरण के रूप में, यह है:

4 // the first defined int 
2063597568 // undefined 
8060928 // undefined 
31488 // undefined 
123 // the second defined int 
268435456 // undefined 
1175453696 // undefined 
-465170432 // undefined 
संबंधित मुद्दे