में एक समय में एक शब्द वापस करें। यह कोड सफेद स्थान को छोड़ने और एक समय में एक शब्द को वापस करने वाला माना जाता है। इस कोड पर कुछ प्रश्न: जब कोड * शब्द ++ = c पर मिलता है; लाइन मुझे कोर डंप मिलता है। क्या मैंने इस रेखा को सही ढंग से लिखा है? और सही वापस आ गया है। और क्या मुझे किसी शब्द को संग्रहीत करने के लिए स्मृति आवंटित करने की आवश्यकता है?सफेद स्थान छोड़ें और सी
//get_word
int get_word(char *word,int lim){
int i=0;
int c;
int quotes=0;
int inword = 1;
while(
inword &&
(i < (lim-1)) &&
((c=getchar()) != EOF)
){
if(c==('\"')){//this is so i can get a "string"
if (quotes) {
inword = 0;
}
quotes = ! quotes;
}
else if(quotes){ //if in a string keep storing til the end of the string
*word++=c;//pointer word gets c and increments the pointer
i++;
}
else if(!isspace(c)) {//if not in string store
*word++=c;
i++;
}
else {
// Only end if we have read some character ...
if (i)
inword = 0;
}
}
*word='\0'; //null at the end to signify
return i; //value
}
क्या आपको दिमाग होगा यदि मैं 'malloc' के वापसी मूल्य से कास्ट हटा देता हूं? –
@R .., इसके बिना मुझे' malloc 'के बाद संकलन त्रुटि मिलती है 'रिटर्न' शून्य * ': त्रुटि C2440: 'प्रारंभ करना': 'शून्य *' से 'char *' में कनवर्ट नहीं किया जा सकता है 'शून्य *' से रूपांतरण 'पॉइंटर से गैर-'void' में एक स्पष्ट कास्ट की आवश्यकता होती है" –
@Steve: फिर आप शायद सी कोड संकलित करने के लिए सी ++ कंपाइलर का उपयोग कर रहे हैं :) – fredoverflow