Memory error, access violation?

For scanf for a number you need to specify the pointer to the number not the number itself. I.e.

Up vote 2 down vote favorite 1 share g+ share fb share tw.

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 first21; char last21; char title21; 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 #include #include 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 #include int main () { char decision; struct Employee emp; struct Employee* emps3; for ( int I = 0; I \nFirstname:"); scanf("%s", emp. First); printf("Lastname:"); scanf("%s", emp. Last); printf("Title:"); scanf("%s", emp.

Title); printf("Salary:"); scanf("%d", &emp. Salary); empsi = 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(emps1); } } 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(emps1);, I get the same kind of error with access violation. C link|improve this question edited 7.1. at 20:14 asked 7.1. at 15:25Ordo234111 91% accept rate.

1 You should try stepping through it with a debugger to find out where the problem is. The two answers posted so far point out one possible place. – Noufal Ibrahim Dec 23 '10 at 15:29 Just a general hint: if you build your code with -Wall, usually you can catch lot of simple mistakes regarding scanf(), printf() and struct.

Field instead of struct->field – Francesco Laurita Dec 23 '10 at 16:18 Sorry I don't know what you mean. What is Wall? – Ordo Dec 23 '10 at 16:27 -Wall is a gcc compile option that turns on lots of warnings.

Use it and pay heed. Other compilers probably have a similar option. – Fred Larson Dec 23 '10 at 16:39.

For scanf for a number you need to specify the pointer to the number not the number itself. I.e. Scanf("%d", &emp.

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

Thank you. It works now. Only the printEmployee(empsi) doesn't work but I think I can fix that on my own.

– Ordo Dec 23 '10 at 15:32 It should be compulsory to post a comment when down-voting. Why would anyone down-vote this answer? Did I get anything wrong?

– AlastairG Dec 23 '10 at 15:38 @AlastairG I don't see a down-vote for you. (Not sure who else down-voted me though. ) – chrisaycock Dec 23 '10 at 15:41 I did no down vote.

– Ordo Dec 23 '10 at 15:46 Sorry, I must take away the accept answer since I still get an memory error when I want to print out the employee data. – Ordo Dec 23 '10 at 15:49.

Thank you. This is definetly wrong but the error is still the same. – Ordo Dec 23 '10 at 16:04 @Ordo Ok, found another issue involving your createEmployee().

My answer has been updated. – chrisaycock Dec 23 '10 at 16:09.

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

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.

Emp. First is a string. For strings you pass the buffer pointer itself, not a pointer to that.

In other words he got that right and your answer is wrong. -1 but I'll remove it if you correct your answer (and I see that in time). – AlastairG Dec 23 '10 at 15:30 @AlastairG Ah, right... – chrisaycock Dec 23 '10 at 15:32 no worries, -1 removed.

– AlastairG Dec 23 '10 at 15:39.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions