तो, मेरे पास कुछ कोड हैं, जैसे कि स्ट्रक्चर की सूची में एक स्ट्रक्चर जोड़ने के लिए:मैं एक पॉइंटर को कैसे संशोधित करूं जो सी में किसी फ़ंक्शन में पास हो गया हो?
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 को इंगित करता हूं, बारपश के अंदर बनाए गए संशोधित संस्करण नहीं। क्या मुझे सूचक को एक सूचक के रूप में पास करना होगा यदि मैं इसे संशोधित करना चाहता हूं, या क्या कोई अन्य अंधेरा अवशोषण आवश्यक है?
धन्यवाद, मुझे लगा कि यह समस्या थी, लेकिन उम्मीद थी कि यह नहीं था;) –
वैकल्पिक रूप से, फ़ंक्शन को सूची के नए सिर पर सूचक वापस लौटाएं। बारलिस्ट * बारपश (बारलिस्ट * सूची, बार * बार) –