C 内存错误,访问冲突
我正在自学C语言,作为练习,我写了一个程序,但它不起作用。该程序分为三个部分。头文件,用于执行程序的主文件,以及用于定义函数的文件。我还没有使用所有的函数,但这应该不是问题所在 这是我的头文件,没有什么特别的C 内存错误,访问冲突,c,C,我正在自学C语言,作为练习,我写了一个程序,但它不起作用。该程序分为三个部分。头文件,用于执行程序的主文件,以及用于定义函数的文件。我还没有使用所有的函数,但这应该不是问题所在 这是我的头文件,没有什么特别的 #ifndef EMPLOYEE_H #define EMPLOYEE_H struct Employee { char first[21]; char last[21]; char title[21]; int salary; }; struc
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
struct Employee
{
char first[21];
char last[21];
char title[21];
int salary;
};
struct Employee* createEmployee(char*, char*, char*, int); // Creates a struct Employee object on the heap.
char* getfirstname (struct Employee*);
char* getlastname (struct Employee*);
char* gettitle (struct Employee*);
int getsalary (struct Employee*);
void setfirstname (struct Employee*, char*);
void setlastname (struct Employee*, char*);
void settitle (struct Employee*, char*);
void setsalary (struct Employee*, int);
void printEmployee(struct Employee*);
#endif
在此文件中,我定义了函数及其工作方式:
#include "7.1.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Employee* createEmployee(char* first, char* last, char* title, int salary) // Creates a struct Employee object on the heap.
{
struct Employee* p = (struct Employee*) malloc(sizeof(struct Employee));
if (p != NULL)
{
strcpy(p->first, first);
strcpy(p->last, last);
strcpy(p->title, title);
p->salary = salary;
}
return p;
}
char* getfirstname (struct Employee* p)
{
if (p != NULL)
return p ? p->first : "";
}
char* getlastname (struct Employee* p)
{
if (p != NULL)
return p ? p->last : "";
}
char* gettitle (struct Employee* p)
{
if (p != NULL)
return p ? p->title : "";
}
int getsalary (struct Employee* p)
{
if (p != NULL)
return p ? p->salary : 0;
}
void setfirstname (struct Employee* p, char* first)
{
if (p != NULL)
strcpy(p->first, first);
}
void setlastname (struct Employee* p, char* last)
{
if (p != NULL)
strcpy(p->last, last);
}
void settitle (struct Employee* p, char* title)
{
if (p != NULL)
strcpy(p->title, title);
}
void setsalary (struct Employee* p, char* salary)
{
if (p != NULL)
p->salary = salary;
}
void printEmployee(struct Employee* p)
{
if (p != NULL)
{
printf("%s, %s, %s, %d",
p->first,
p->last,
p->salary,
p->salary
);
}
}
最后一个文件用于执行程序/功能:
#include "7.1.h"
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char decision;
struct Employee emp;
struct Employee* emps[3];
for ( int i = 0; i < 1; i ++)
{
printf("Please type in the emplooyes data.\nFirstname:");
scanf("%s", emp.first);
printf("Lastname:");
scanf("%s", emp.last);
printf("Title:");
scanf("%s", emp.title);
printf("Salary:");
scanf("%d", &emp.salary);
emps[i] = createEmployee(emp.first, emp.last, emp.title, emp.salary);
}
printf("Do you want to print out your information? (Y/N):");
scanf("%c", &decision);
if (decision == 'y' || decision == 'Y')
{
printEmployee(emps[1]);
}
}
我不知道是什么问题。在第一次输入first、last、title和salary之后,我总是收到以下错误消息。错误是用德语写的。这意味着:
7.1.exe中0x102de42e msvcr100d.dll处未处理的异常:0xC0000005:写入0xCCCC位置时发生访问冲突
我可以用下面给出的提示解决第一个问题。现在,当我想使用函数printEmployeeemps[1];,打印员工数据时;,我遇到了与访问冲突相同的错误 对于数字的scanf,您需要指定指向数字的指针,而不是数字本身。即
scanf("%d", &emp.salary);
可能还有其他问题,但这是我看到的第一个问题。这就是您对scanf的称呼: 你应该这样称呼scanf:
编辑:正如AlstairG指出的,您的字符串char*成员是指针,因此已经有了地址。不知道这是否是唯一的问题,但一个问题是: scanf%d,emp.工资 哪一个是错误的忘记了工资上的地址& 最好尝试:扫描%d,&emp.salary ++您的printf参数看起来不合适:
printf("%s, %s, %s, %d",
p->first,
p->last,
p->salary, // did you mean title here rather than salary?
p->salary
);
此外,您还希望在格式字符串的末尾包含新行字符:
printf("%s, %s, %s, %d\n", // note the \n
...
您的createEmployee定义也有问题:
p->salary, salary; // did you mean p->salary = salary?
您应该尝试使用调试器单步执行,以找出问题所在。到目前为止发布的两个答案指出了一个可能的地方。只是一个一般性的提示:如果你用-Wall构建代码,通常你会发现很多关于scanf、printf和struct.field的简单错误,而不是struct->field对不起,我不知道你的意思。什么是墙?-墙是一个gcc编译选项,可以打开很多警告。使用它并注意。其他编译器可能也有类似的选项。emp.first是字符串。对于字符串,传递的是缓冲区指针本身,而不是指向它的指针。换句话说,他是对的,而你的答案是错的-1但如果你及时更正你的答案,我会删除它。谢谢。现在可以了。只有printEmployeeemps[i]不起作用,但我想我可以自己解决这个问题。投票时必须发表评论。为什么会有人投票反对这个答案?我做错什么了吗?@AlastairG我看不到对你投反对票。但不确定还有谁投票反对我。对不起,我必须删除“接受”答案,因为我在打印员工数据时仍然出现内存错误。@要查看第二个答案以供打印谢谢。这绝对是错误的,但错误仍然是相同的。@Ordo Ok,发现与createEmployee有关的另一个问题。我的答案已经更新了。
printf("%s, %s, %s, %d\n", // note the \n
...
p->salary, salary; // did you mean p->salary = salary?