gpt4 book ai didi

c - 传递 'new_student' 的参数 1 使指针来自整数而不进行强制转换

转载 作者:太空宇宙 更新时间:2023-11-04 08:41:23 25 4
gpt4 key购买 nike

我正在尝试编辑一个结构数组。用户可以输入 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;
}

'

这是我用来分隔 id 和名称的方法

    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 of 'new_student' makes pointer from integer 而没有强制转换警告。

最佳答案

问题分析

根据签名

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,而被调用者需要 char *

解决方案草图

拆分字符串是一项相当常见的任务,在推出复杂的解决方案之前,请花一些时间(重新)搜索它。

C

C++

关于c - 传递 'new_student' 的参数 1 使指针来自整数而不进行强制转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23393541/

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