#include <stdio.h>
#include <string.h>
struct student
{
char StudentName[50];
char StudentMajor[4];
double StudentGPA;
double StudentCredits;
char StudentID[9];
};
void main()
{
struct student Student;
strcpy(Student.StudentName, "Christian Gigliotti");
strcpy(Student.StudentMajor, "TECH");
strcpy(Student.StudentGPA, "2.4");
strcpy(Student.StudentCredits, "31");
strcpy(Student.StudentID, "J02062414");
printf("Student's Name: %s\n", Student.StudentName);
printf("Student's Major: %s\n", Student.StudentMajor);
printf("Student's GPA: %d\n", Student.StudentGPA);
printf("Student's Credits Earned: %d\n", Student.StudentCredits);
printf("Student's ID: %s\n", Student.StudentID);
return 0;
}
我在第24和25行得到error C2440: 'function': cannot convert from 'double' to 'char*'
。
我认为这与我试图用于gpa&amp;的双重问题有关。学分。我不明白为什么它会影响那些线条。
答案 0 :(得分:2)
你不能使用:
strcpy(Student.StudentGPA, "2.4");
strcpy(Student.StudentCredits, "31");
因为Student.StudentGPA
和Student.StudentCredits
属于double
类型。
使用:
Student.StudentGPA = 2.4;
Student.StudentCredits = 31;
BTW,该行
strcpy(Student.StudentMajor, "TECH");
将导致未定义的行为,因为Student.StudentMajor
没有足够的空间来容纳这些字符和终止空字符。使Student.StudentMajor
的大小至少为5。
strcpy(Student.StudentID, "J02062414");
遇到同样的问题。使Student.StudentID
的大小至少为10。