gpt4 book ai didi

python - 循环添加学生

转载 作者:行者123 更新时间:2023-11-30 16:58:18 25 4
gpt4 key购买 nike

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 异常访问冲突写入 ..... in 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'

最佳答案

您为 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);
}

关于python - 循环添加学生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38971652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com