简体   繁体   中英

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. 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. 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.

替代文字

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]); , 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. Ie

scanf("%d", &emp.salary);

There may be other problems, but that's the first one I saw.

Your printf() arguments look off:

        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:

    p->salary, salary;  // did you mean p->salary = salary?

This is how you're calling scanf() :

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

This is how you're supposed to call 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.

don't know, if it's the only issue, but one trouble is :

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

Which is wrong (forgot address on salary with &)

better try : scanf("%d", &emp.salary);

++

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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