2009-04-20 4 views
34

तो, मेरे पास कुछ कोड हैं, जैसे कि स्ट्रक्चर की सूची में एक स्ट्रक्चर जोड़ने के लिए:मैं एक पॉइंटर को कैसे संशोधित करूं जो सी में किसी फ़ंक्शन में पास हो गया हो?

void barPush(BarList * list,Bar * bar) 
{ 
    // if there is no move to add, then we are done 
    if (bar == NULL) return;//EMPTY_LIST; 

    // allocate space for the new node 
    BarList * newNode = malloc(sizeof(BarList)); 

    // assign the right values 
    newNode->val = bar; 
    newNode->nextBar = list; 

    // and set list to be equal to the new head of the list 
    list = newNode; // This line works, but list only changes inside of this function 
} 

इन संरचनाओं को निम्नानुसार परिभाषित किया गया है:

typedef struct Bar 
{ 
    // this isn't too important 
} Bar; 

#define EMPTY_LIST NULL 

typedef struct BarList 
{ 
    Bar * val; 
    struct BarList * nextBar; 
} BarList; 

और फिर दूसरी फ़ाइल में मैं निम्नलिखित की तरह कुछ करता हूं:

BarList * l; 

l = EMPTY_LIST; 
barPush(l,&b1); // b1 and b2 are just Bar's 
barPush(l,&b2); 

हालांकि, इसके बाद, मैं अभी भी EMPTY_LIST को इंगित करता हूं, बारपश के अंदर बनाए गए संशोधित संस्करण नहीं। क्या मुझे सूचक को एक सूचक के रूप में पास करना होगा यदि मैं इसे संशोधित करना चाहता हूं, या क्या कोई अन्य अंधेरा अवशोषण आवश्यक है?

उत्तर

41

आप एक सूचक के लिए सूचक में पारित करने के लिए की जरूरत है अगर आप ऐसा करना चाहते हैं की तरह एक सूचक के लिए सूचक में गुजरती हैं,।

void barPush(BarList ** list,Bar * bar) 
{ 
    if (list == NULL) return; // need to pass in the pointer to your pointer to your list. 

    // if there is no move to add, then we are done 
    if (bar == NULL) return; 

    // allocate space for the new node 
    BarList * newNode = malloc(sizeof(BarList)); 

    // assign the right values 
    newNode->val = bar; 
    newNode->nextBar = *list; 

    // and set the contents of the pointer to the pointer to the head of the list 
    // (ie: the pointer the the head of the list) to the new node. 
    *list = newNode; 
} 

तो इस तरह इसका इस्तेमाल:

BarList *barPush(BarList *list,Bar *bar) 
{ 
    // if there is no move to add, then we are done - return unmodified list. 
    if (bar == NULL) return list; 

    // allocate space for the new node 
    BarList * newNode = malloc(sizeof(BarList)); 

    // assign the right values 
    newNode->val = bar; 
    newNode->nextBar = list; 

    // return the new head of the list. 
    return newNode; 
} 

प्रयोग हो जाता है::

BarList * l; 

l = EMPTY_LIST; 
l = barPush(l,&b1); // b1 and b2 are just Bar's 
l = barPush(l,&b2); 
+1

धन्यवाद, मुझे लगा कि यह समस्या थी, लेकिन उम्मीद थी कि यह नहीं था;) –

+3

वैकल्पिक रूप से, फ़ंक्शन को सूची के नए सिर पर सूचक वापस लौटाएं। बारलिस्ट * बारपश (बारलिस्ट * सूची, बार * बार) –

2

हां, आपको पॉइंटर को पॉइंटर में पास करना होगा। सी संदर्भ द्वारा तर्क नहीं, मूल्य से तर्क पास करता है।

6

याद रखें, सी में, सबकुछ मूल्य से गुजरता है।

आप इस

int myFunction(int** param1, int** param2) { 

// now I can change the ACTUAL pointer - kind of like passing a pointer by reference 

} 
2

BarList * l; 

l = EMPTY_LIST; 
barPush(&l,&b1); // b1 and b2 are just Bar's 
barPush(&l,&b2); 

जोनाथन Leffler टिप्पणी में सूची के नए प्रमुख लौटने का सुझाव दिया

यह एक क्लासिक पी है roblem। या तो आवंटित नोड वापस करें या सूचक के सूचक का उपयोग करें। सी में, आपको एक एक्स को एक फ़ंक्शन पर एक पॉइंटर पास करना चाहिए जहां आप अपने एक्स को संशोधित करना चाहते हैं। इस मामले में, चूंकि आप एक सूचक को संशोधित करना चाहते हैं, इसलिए आपको एक सूचक को एक सूचक को पास करना होगा।

14

सामान्य उत्तर: उस चीज़ को पॉइंटर पास करें जिसे आप बदलना चाहते हैं।

इस मामले में, यह उस सूचक के लिए सूचक होगा जो आप बदलना चाहते हैं।

संबंधित मुद्दे