【问题标题】:Getting error C2440收到错误 C2440
【发布时间】:2015-03-13 23:03:32
【问题描述】:
#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。

标签: c compiler-errors


【解决方案1】:

你不能使用:

strcpy(Student.StudentGPA, "2.4");
strcpy(Student.StudentCredits, "31");

因为Student.StudentGPAStudent.StudentCredits 的类型是double

用途:

Student.StudentGPA = 2.4;
Student.StudentCredits = 31;

顺便说一句,这条线

strcpy(Student.StudentMajor, "TECH");

将导致未定义的行为,因为Student.StudentMajor 没有足够的空间来容纳这些字符和终止的空字符。使Student.StudentMajor的大小至少为5。

strcpy(Student.StudentID, "J02062414");

遇到同样的问题。使Student.StudentID 的大小至少为 10。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 2020-03-01
    • 1970-01-01
    相关资源
    最近更新 更多