suresh
suresh

Reputation: 11

Access Violation error in Pointers in C

This is the simple function month_day which converts the day of the year into the month and day. This is from the C tutorial with some modifications.

I dont know,when I run this I get

"....... Access violation at address 004011DD ....." error stopping at
*month=i;    //in daytellerlib.c file.

This is the complete program in the two files. File 1:

#include<stdio.h>
#include<conio.h>
#include"daytellerlib.c"

int main()
{
int dayyear,year;
int *pday=0;
int *pmonth=0;

dayyear=99;
year=2011;

month_day(dayyear,year,pmonth,pday);
printf("dayyear=%d  YEar=%d  day=%d  month=%d \n",dayyear,year,*pday,*pmonth);
getch();
return 0;
}

File 2:

void month_day(int day,int year,int *,int *);


static table[2][13]={
 {0,31,28,31,30,31,30,31,31,30,31,30,31},
 {0,31,29,31,30,31,30,31,31,30,31,30,31}
};


void month_day(int dayyear,int year,int *month,int *day)
{
int i=1,count=0,leap;

leap = year%4 == 0 &year%100 != 0 || year%400 == 0;
for(;dayyear>(count+=table[leap][i]);i++)
 ;
*month=i;
*day=dayyear+table[0][i]-count;                    
}

I know this is because we are accessing the pointer which has some other address.

Upvotes: 1

Views: 1266

Answers (1)

Mitch Wheat
Mitch Wheat

Reputation: 300559

When you call month_day() you are passing pointers to ints which have no ints associated with them.

int day; 
int month; 
int * pday = &day; 
int * pmonth = &month; 

or more simply

int day; 
int month; 

month_day(dayyear, year, &month, &day); 

Update: as @Eran Zimmerman points out, you should use && (logical AND) instead of & (bitwise AND) when calculating leap.

Upvotes: 8

Related Questions