【发布时间】:2018-01-30 01:09:16
【问题描述】:
我的代码中有以下函数来提取名称:
void student_init(struct student *s, char *info) {
char *name;
char *name2;
char a[] = { *info };
name = strtok(a, "/");
strcpy(s->first_name, name);
name2 = strtok(NULL, "/");
strcpy(s->last_name, name2);
}
当我运行它时,我看到:
Please enter a student information or enter "Q" to quit.
Daisy/Lee
A student information is read.: Daisy/Lee
Please enter a row number where the student wants to sit.
1
Please enter a column number where the student wants to sit.
2
The seat at row 1 and column 2 is assigned to the student: D . �
?.?. ?.?. ?.?.
?.?. ?.?. D.�.
?.?. ?.?. ?.?.
我正在尝试在 c 程序中使用 strtok 函数用“/”分割字符串以分隔拳头和姓氏,并将它们存储在学生结构的 first_name 和 last_name 变量中。我可以获取存储在相应变量中的名字,但正如您从上面链接中的图像中看到的那样,我得到了一个 ?输出中姓氏的第一个首字母应该是的符号。
【问题讨论】:
-
欢迎来到 SO!请阅读tour 然后How to Ask 然后minimal reproducible example 然后编辑您的问题。
-
请不要张贴代码和输出的图片,我们不能从图片中复制粘贴代码并自行复制。
-
char a[] = { *info };不会像您认为的那样做。由于info是char *,*info是单个字符。