2010-11-13 14 views
15

मुझे structs की सरणी शुरू करने में समस्या आ रही है। मुझे यकीन नहीं है कि मैं इसे सही कर रहा हूं क्योंकि मुझे "असंगत सूचक प्रकार से प्रारंभिकता" & "असंगत सूचक प्रकार से असाइनमेंट" मिलता है। मैं कोड है जहाँ मैं इन चेतावनियों पाने में जोड़ा, और जब मैं struct से डेटा मुद्रित करने के लिए कोशिश मैं सिर्फ // सरणी प्रारंभसी - structs की सरणी शुरू करें

student** students = malloc(sizeof(student)); 
    int x; 
    for(x = 0; x < numStudents; x++) 
    { 
     //here I get: "assignment from incompatible pointer type" 
     students[x] = (struct student*)malloc(sizeof(student)); 
    } 

    int arrayIndex = 0; 
ऐसे @@ ###

typedef struct 
{ 
    char* firstName; 
    char* lastName; 
    int day; 
    int month; 
    int year; 

}student; 

के रूप में कचरा मिल

// struct जोड़ने

//create student struct 
     //here I get: "initialization from incompatible pointer type" 
     student* newStudent = {"john", "smith", 1, 12, 1983}; 

     //add it to the array 
     students[arrayIndex] = newStudent; 
     arrayIndex++; 
+1

आपका कोड गतिशील रूप से छात्र structs की एक सरणी बनाने और छात्र संरचनाओं के लिए पॉइंटर्स की एक सरणी बनाने के बीच आधा रास्ता लगता है, और फिर गतिशील रूप से प्रत्येक छात्र संरचना को बनाते हैं जो इंगित करता है। यह स्पष्ट नहीं है कि आप कौन सी कोशिश कर रहे हैं, जो इस मुश्किल का जवाब देता है। –

उत्तर

25
student** students = malloc(sizeof(student)); 

नहीं, नहीं, नहीं!

आप ** नहीं चाहते हैं। आप के लिए एक * और पर्याप्त जगह चाहते हैं आप कैसे कभी कई छात्रों की जरूरत है

student* students = malloc(numStudents * sizeof *students); 
for (x = 0; x < numStudents; x++) 
{ 
    students[x].firstName = "John"; /* or malloc and strcpy */ 
    students[x].lastName = "Smith"; /* or malloc and strcpy */ 
    students[x].day = 1; 
    students[x].month = 12; 
    students[x].year = 1983; 
} 
+1

निश्चित रूप से आपका मतलब है 'छात्र * छात्र = malloc (numStudents * sizeof (छात्र))', है ना? – GnP

+1

@ जीएनपी: मैं ऑब्जेक्ट को 'sizeof' ऑपरेटर के लिए तर्क के रूप में उपयोग करना पसंद करता हूं। 'स्ट्रक्चर कुछ * पीआरटी' के साथ ऑपरेटर को (कोष्ठक) प्रकार 'स्ट्रक्चर कुछ' या ऑब्जेक्ट '* पीआरटी' में लागू करने के लिए समान मूल्य उत्पन्न होता है। 'sizeof * ptr == sizeof (संरचना कुछ)'। – pmg

+0

धन्यवाद @prng। जब तक मुझे एहसास हुआ कि 'sizeof' एक यूनरी ऑपरेटर है और सी – GnP

2
student* students = malloc(sizeof(student)*numStudents); 
int x; 
for(x = 0; x < numStudents; x++) 
{ 
    student newStudent = {"john", "smith", 1, 12, 1983}; // string copy are wrong still 
    students[x] = newStudent; 
} 
7

संकलक चेतावनी से संबंधित नहीं है, लेकिन अपने प्रारंभिक malloc गलत है; आप चाहते हैं:

malloc(sizeof(student *)* numStudents) 

छात्र को कुल 'numStudents' पॉइंटर्स के लिए कमरे आवंटित करने के लिए। रेखा:

students[x] = (struct student*)malloc(sizeof(student)); 

होना चाहिए:

students[x] = (student*)malloc(sizeof(student)); 

'struct छात्र' जैसी कोई चीज नहीं है। आपने एक अनाम नाम घोषित कर दिया है और इसे 'छात्र' में टाइप किया है। तुलना करें और के साथ इसके विपरीत:

struct student 
{ 
    char* firstName; 
    char* lastName; 
    int day; 
    int month; 
    int year; 

}; 

जो एक 'struct छात्र' प्रकार बनाने, लेकिन आप की आवश्यकता होती है (सी में) स्पष्ट रूप से केवल कहीं और छात्र के बजाय छात्र struct का उल्लेख होगा। यह नियम C++ के लिए बदला गया है, इसलिए आपका कंपाइलर इसके बारे में थोड़ा अस्पष्ट हो सकता है।

के लिए के रूप में:

student* newStudent = {"john", "smith", 1, 12, 1983}; 

होना चाहिए कि:

student newStudent = {"john", "smith", 1, 12, 1983}; 

घुंघराले ब्रेस वाक्य रचना के रूप में एक सीधा शाब्दिक, नहीं कुछ कहीं और है कि आप को इंगित करने की जरूरत है।

संपादित करें: प्रतिबिंब पर, मुझे लगता है कि aaa ने मेरे पास इसके बारे में अधिक जानकारी ली है। क्या यह संभव है कि आप अनजाने में पॉइंटर अव्यवस्था के अतिरिक्त स्तर का उपयोग कर अनजाने में हैं? तो अगर आप चाहते हैं:

student* students = malloc(sizeof(student) * numStudents); 

/* no need for this stuff: */ 
/*int x; 
for(x = 0; x < numStudents; x++) 
{ 
    //here I get: "assignment from incompatible pointer type" 
    students[x] = (struct student*)malloc(sizeof(student)); 
}*/ 

int arrayIndex = 0; 

और:

student newStudent = {"john", "smith", 1, 12, 1983}; 

//add it to the array 
students[arrayIndex] = newStudent; 
arrayIndex++; 

विषय सरणी newStudent के दायरे से बाहर नहीं किया जा रहा। अन्यथा पॉइंटर्स को तारों पर कॉपी करना गलत है।

-1

जब आरंभ, यह इस तरह shoudn't?

student** students = (struct student**)malloc(sizeof(student*)*numStudents); 

हालांकि, सूचक के लिए पॉइंटर क्यों? संरचना के लिए एक सूचक के साथ बस मुझे लगता है कि पर्याप्त है।

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