获取错误 C2440
Getting error C2440
#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 和学分的双倍有关。我不明白为什么它会影响这些线条。
您不能使用:
strcpy(Student.StudentGPA, "2.4");
strcpy(Student.StudentCredits, "31");
因为 Student.StudentGPA
和 Student.StudentCredits
的类型是 double
。
使用:
Student.StudentGPA = 2.4;
Student.StudentCredits = 31;
顺便说一句,行
strcpy(Student.StudentMajor, "TECH");
将导致未定义的行为,因为 Student.StudentMajor
没有足够的 space 来保存这些字符和终止空字符。使 Student.StudentMajor
的大小至少为 5。
strcpy(Student.StudentID, "J02062414");
遇到同样的问题。使 Student.StudentID
的大小至少为 10。
#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 和学分的双倍有关。我不明白为什么它会影响这些线条。
您不能使用:
strcpy(Student.StudentGPA, "2.4");
strcpy(Student.StudentCredits, "31");
因为 Student.StudentGPA
和 Student.StudentCredits
的类型是 double
。
使用:
Student.StudentGPA = 2.4;
Student.StudentCredits = 31;
顺便说一句,行
strcpy(Student.StudentMajor, "TECH");
将导致未定义的行为,因为 Student.StudentMajor
没有足够的 space 来保存这些字符和终止空字符。使 Student.StudentMajor
的大小至少为 5。
strcpy(Student.StudentID, "J02062414");
遇到同样的问题。使 Student.StudentID
的大小至少为 10。