繁体   English   中英

内存错误,访问冲突

[英]Memory error, access violation

我自己学习C,作为练习,我编写了一个程序,但是它不起作用。 该程序分为三个部分。 头文件,用于执行程序的主文件,用于定义功能的文件。 我尚未使用所有功能,但这不应该成为问题。

这是我的头文件,没有什么特别的。

#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]);

    }

}

我不知道问题是什么。 我在第一,最后,职位和薪水上一次键入M后总是收到以下错误消息。 错误是用德语写的。 它的意思是:
7.1.exe中0x102de42e(msvcr100d.dll)的未处理异常:0xC0000005:写入0xCCCCCCCC位置时发生访问冲突。

替代文字

我可以通过下面给出的提示解决第一个问题。 现在,当我想使用以下功能打印出员工数据时: printEmployee(emps[1]); ,我遇到与访问冲突相同的错误。

对于数字的scanf,您需要指定指向数字的指针,而不是数字本身。

scanf("%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?

这就是您调用scanf()

scanf("%s", emp.salary);

这就是您应该调用scanf()

scanf("%s", &emp.salary);  // note the address to the location you want

编辑:正如AlstairG指出的那样,您的字符串( char*成员)是指针,因此已经具有地址。

不知道,这是否是唯一的问题,但是一个麻烦是:

scanf(“%d”,emp.salary);

错了(用&忘记了工资地址)

更好的尝试: scanf(“%d”,&emp.salary);

++

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM