2016-08-16 111 views
0
void addStudent(char* lastName, 
       char* firstName, 
       char* studentId, 
       smartresponse_classV1_t* signInClass) 
{ 

    sr_student_t *student = sr_student_create(
     lastName, 
     sizeof lastName + 1, 
     firstName, 
     sizeof firstName + 1, 
     studentId, 
     sizeof studentId); 

    sr_class_addstudent(signInClass, student); 

    sr_student_release(student); 
} 

// add student 
char *firstName = "first"; 
char *lastName = "last"; 
char *studentId = "1"; 
char *id =""; 
int i; 

for (i = 1; i < 10; i++) 
{ 
    id = _itoa(i, studentId, 10); 

    addStudent(lastName, firstName, id, signInClass); 
} 

我想將int轉換爲字符串,以便我可以爲新學生分配新ID。 我不知道我在做什麼錯誤,因爲我從python調用測試dll函數,並以某種方式它給了我一個錯誤windowserror異常訪問衝突寫作.....在打印dll.test() 是否有問題在for循環中,當我調用該函數並將id分配給它時?在循環中添加學生

def test(x): 
    ''' Just runs the main test. 
    >>> test(1) 
    1 
    ''' 

    if x == 1: 
     print dll.test() 

if __name__ == '__main__': 
    ''' Testing the library. ''' 
    import doctest 
    if doctest.testmod()[0] > 0: 
     raise Exception('Unit tests have errors') 
    print 'Unit tests OK' 

回答

0

您分配較少的字節到你的ID和studentId指針

char *id =""; //1 byte assigned 0x00 at string "" end 
char *studentId ="1"; // 2 bytes assigned but in code you will need 3 ("10"+null) 

您的代碼應該是這樣的:

void addStudent(char* lastName, char* firstName, char* studentId, smartresponse_classV1_t* signInClass){ 
      sr_student_t *student = sr_student_create(lastName, sizeof lastName + 1, firstName, sizeof firstName + 1, studentId, sizeof studentId); 
      sr_class_addstudent(signInClass, student); 
      sr_student_release(student); 
    } 
char *firstName = "first"; 
char *lastName = "last"; 
char *studentId = "00"; 
char *id ="00"; 
int i; 

for (i = 1; i < 10; i++){ 
    id = _itoa(i, studentId, 10); 
    addStudent(lastName, firstName, id, signInClass); 
} 
+0

非常感謝您的回覆,但問題仍然存在我認爲在調用dll測試函數時存在一個問題。但它會運行,如果我從循環中刪除itoa調用和id並運行它只有學生id =「1」。請幫助我 – sanchaz

+0

如果您刪除了'id = _itoa(i,studentId,10);'並且它運行。你應該在那裏搜索你的問題。 「訪問衝突」意味着你試圖寫一些你不允許的地方。 – Sahee