[英]Memory error, access violation
I'm learning C on my own and as a exercise i have written a program but it does not work. 我自己学习C,作为练习,我编写了一个程序,但是它不起作用。 The program is splitted into 3 parts.
该程序分为三个部分。 A header file, a main file for executing the program a file to define the functions.
头文件,用于执行程序的主文件,用于定义功能的文件。 I'm not using all the functions yet but that shouldn't be the problem.
我尚未使用所有功能,但这不应该成为问题。
Here is my header file, nothing special in it. 这是我的头文件,没有什么特别的。
#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
In this file i define the functions and how they work: 在此文件中,我定义了功能及其工作方式:
#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
);
}
}
And the last file is used to executed the program/functions: 最后一个文件用于执行程序/功能:
#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]);
}
}
I don't know what the problem is. 我不知道问题是什么。 I 'm always getting the following error message after typing in first, last, title and salary for the first time.
我在第一,最后,职位和薪水上的第一次键入M后总是收到以下错误消息。 The error is written in german.
错误是用德语写的。 It means:
它的意思是:
Unhandled exception at 0x102de42e (msvcr100d.dll) in 7.1.exe: 0xC0000005: Access violation when writing to 0xCCCCCCCC position. 7.1.exe中0x102de42e(msvcr100d.dll)的未处理异常:0xC0000005:写入0xCCCCCCCC位置时发生访问冲突。
I could fix the first problem with the hints given below. 我可以通过下面给出的提示解决第一个问题。 Now when i want to print out the employee data using the function:
printEmployee(emps[1]);
现在,当我想使用以下功能打印出员工数据时:
printEmployee(emps[1]);
, I get the same kind of error with access violation. ,我遇到与访问冲突相同的错误。
For scanf for a number you need to specify the pointer to the number not the number itself. 对于数字的scanf,您需要指定指向数字的指针,而不是数字本身。 Ie
即
scanf("%d", &emp.salary);
There may be other problems, but that's the first one I saw. 可能还有其他问题,但这是我看到的第一个问题。
Your printf()
arguments look off: 您的
printf()
参数看起来像:
printf("%s, %s, %s, %d",
p->first,
p->last,
p->salary, // did you mean title here rather than salary?
p->salary
);
Also, you'll want to include new line character at the end of your format string: 另外,您还需要在格式字符串的末尾添加换行符:
printf("%s, %s, %s, %d\n", // note the \n
...
Your createEmployee()
definition is also questionable: 您的
createEmployee()
定义也值得怀疑:
p->salary, salary; // did you mean p->salary = salary?
This is how you're calling scanf()
: 这就是您调用
scanf()
:
scanf("%s", emp.salary);
This is how you're supposed to call scanf()
: 这就是您应该调用
scanf()
:
scanf("%s", &emp.salary); // note the address to the location you want
EDIT: As AlstairG points out, your strings (the char*
members) are pointers and thus have the address already. 编辑:正如AlstairG指出的那样,您的字符串(
char*
成员)是指针,因此已经具有地址。
don't know, if it's the only issue, but one trouble is : 不知道,这是否是唯一的问题,但是一个麻烦是:
scanf("%d", emp.salary); scanf(“%d”,emp.salary);
Which is wrong (forgot address on salary with &) 错了(用&忘记了工资地址)
better try : scanf("%d", &emp.salary); 更好的尝试: scanf(“%d”,&emp.salary);
++ ++
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.