我试图编辑一个structs.the用户数组,它能够输入3个数字,每个数字执行不同的操作。例如,您可以通过键入:
1
1234 marvin这方面的所有代码如下
void input_interpreter()
{
int input;
char inputc1[106];
scanf("%d", &input);
switch(input)
{
case 0 :
/*pretty self explanatory*/
exit(0);
break;
case 1 :
/*add a student to the array*/
scanf("%s", inputc1);
new_student((string_split_string(inputc1),(string_split_int(inputc1)));/*<----the warning points here*/
break;
case 2 :
/*Remove a specified student, (implicitly unenrolling them from all their units, if any), O(S + U).*/
remove();
break;
case 3 :
/*Print, in ascending numerical order of ID number, the ID numbers and names of the students in
the database, O(S).*/
print_array();
break;
}
input_interpreter();
return;
}‘
这是我用来区分身份证和名字的东西
int string_split_int(char input_string[])
{
char * ptr;
int ID = 1;
int ch = " ";
int i;
int name_start;
int array_length = sizeof(input_string);
ptr = strchr(input_string, ch);
name_start = array_length - sizeof(ptr); /*may have to change this if names are including namespaces*/
for(i = name_start; i >= array_length; i--)
{
ID=ID/10;
ID=ID+input_string[i];
}
return ID;
}
char string_split_string(char input_string[])
{
char * ptr;
char name[100];
int ch = ' ';
int i;
int name_start;
int array_length = sizeof(input_string);
ptr = strchr(input_string, ch);
name_start = array_length - sizeof(ptr); /*may have to change this if names are including namespaces*/
for(i = name_start; i <= array_length; i++)
{
name[i] = input_string[i];
}
return *name;
}
void new_student(char *name, int ID)
{
struct student s;
s.ID=ID;
s.name=name;
insert_array(s);
return;
}不幸的是,这抛出了一个传递参数1的'new_student‘使指针从整数没有强制转换警告。
发布于 2014-04-30 16:47:11
问题分析
根据签名
void new_student(char *name, int ID)第一个参数必须是char *。
不过,根据电话
new_student((string_split_string(inputc1),(string_split_int(inputc1)));
/*<----the warning points here*/和每一个签名
char string_split_string(char input_string[]);string_split_string()返回的类型,因此new_student()的第一个参数是char。
简而言之,调用者提供的是char ,被调用者需要。
溶液素描
拆分字符串是一项相当常见的任务,在推出复杂的解决方案之前,请先对其进行(重新)搜索。
C
C++
std::string及其成员函数,例如std::string::find()https://stackoverflow.com/questions/23393541
复制相似问题