循环添加学生

时间:2016-08-16 09:48:14

标签: python c

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异常访问违规写入.....在print dll.test() 当我调用函数并为其分配id时,for循环中是否存在问题?

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'

1 个答案:

答案 0 :(得分: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);
}