none
Access violation writing location 0xcccccccc

    Question

  • So, I'm writing code where I have to set inputs to a struct, and I am getting the error 
    "Unhandled exception at 0x10296af2 (msvcr90d.dll) in <my program>: 0xC0000005: Access violation writing location 0xcccccccc." When trying to scanf to an int inside struct "a". I have no error when using scanf on chars.



    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    	
    typedef struct  
    {
    	char employee_name[80];
    	int employee_ID;
    	int position_code;
    	int years_exp;
    	double salary;
    	double bonus;
    }employee_data;
    
    employee_data data_input()
    {
    	employee_data a;
    	printf("Enter employee name: ");
    	scanf("%s", a.employee_name);
    	printf("Enter employee number: ");
    	scanf("%d", a.employee_ID);
    	printf("Enter position code: ");
    	scanf("%d", a.position_code);
    	printf("Enter years of experience: ");
    	scanf("%d", a.years_exp);
    	printf("Enter employee salary: ");
    	scanf("%f", a.salary);
    	a.bonus=0;
    
    	return (a);
    }

     any idea why I'm getting this problem? I tried initializing a.employee_ID to 0 inside data_input(), but that didn't solve my problem. I don't see why the pointer doesn't initialize since the struct is inside the employee_data type I initialize in data_input().

    • Edited by CKTofu Monday, February 06, 2012 6:06 PM
    Monday, February 06, 2012 6:01 PM

Answers

  • Use instead..
    scanf("%d", &a.position_code);
    scanf("%d", &a.years_exp);
    scanf("%f", &a.salary);

    Regards
    Harsh

    Please Mark reply as "Answered" if they helped
    • Edited by Harsh Shankar Monday, February 06, 2012 6:08 PM
    • Marked as answer by CKTofu Monday, February 06, 2012 6:10 PM
    Monday, February 06, 2012 6:07 PM

All replies

  • Use instead..
    scanf("%d", &a.position_code);
    scanf("%d", &a.years_exp);
    scanf("%f", &a.salary);

    Regards
    Harsh

    Please Mark reply as "Answered" if they helped
    • Edited by Harsh Shankar Monday, February 06, 2012 6:08 PM
    • Marked as answer by CKTofu Monday, February 06, 2012 6:10 PM
    Monday, February 06, 2012 6:07 PM
  • My god, how did I miss that. I knew that too, facepalming at myself so hard. Thanks!
    Monday, February 06, 2012 6:10 PM
  • In addition to the missing & operator previously noted, you have a mismatch between the format specification and the object to receive the value in the last call to scanf.  a.salary is a double so you need to specify %lf, not %f.
    Monday, February 06, 2012 7:05 PM