C中指针中的访问冲突错误
这是一个简单的函数C中指针中的访问冲突错误,c,C,这是一个简单的函数month\u day,它将一年中的某一天转换为月份和日期。这是C教程中的一些修改 我不知道,当我运行这个时 "....... Access violation at address 004011DD ....." error stopping at *month=i; //in daytellerlib.c file. 这是两个文件中的完整程序。文件1: #include<stdio.h> #include<conio.h> #include"
month\u day
,它将一年中的某一天转换为月份和日期。这是C教程中的一些修改
我不知道,当我运行这个时
"....... Access violation at address 004011DD ....." error stopping at
*month=i; //in daytellerlib.c 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;
}
我知道这是因为我们正在访问具有其他地址的指针。当您调用
month\u day()
时,您正在将指针传递给没有与之关联的int
int day;
int month;
int * pday = &day;
int * pmonth = &month;
或者更简单地说
int day;
int month;
month_day(dayyear, year, &month, &day);
更新:正如@Eran Zimmerman所指出的,在计算leap时,应该使用&(逻辑AND)而不是&(按位AND)。当调用
month\u day()
时,您传递的指针指向没有与之关联的整数
int day;
int month;
int * pday = &day;
int * pmonth = &month;
或者更简单地说
int day;
int month;
month_day(dayyear, year, &month, &day);
更新:正如@Eran Zimmerman指出的,在计算闰时,应该使用&&(逻辑和),而不是&(按位和)。计算
lep
,因为您需要逻辑的,而不是一点明智的。在计算lep
时,应该使用&&代替&,因为你想要的是逻辑和,而不是一点明智和。苏雷什:恐怕你的评论毫无意义?---------------------------------------------------------------------------------------谢谢你的回复。因此,对于下面的简单代码void main(){int x=1,y=2,*p;*p=0;//错误。p是指向int的指针,但与任何int printf都没有关联(“p=%d\n”,*p);//错误。p=&x;//与x int printf关联的correct(“将x复制到p=%d\n,*p之后);//correct},我们应该访问与某个变量关联的指针?抱歉之前的编辑。苏雷什:恐怕你的评论毫无意义?---------------------------------------------------------------------------------------谢谢你的回复。因此,对于下面的简单代码void main(){int x=1,y=2,*p;*p=0;//错误。p是指向int的指针,但与任何int printf都没有关联(“p=%d\n”,*p);//错误。p=&x;//与x int printf关联的correct(“将x复制到p=%d\n,*p之后);//correct},我们应该访问与某个变量关联的指针?对不起,前面的编辑。