| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付买域名,送MP3、MP4
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY买空间,免费送域名(厦门中资源)
共有 372 人关注过本帖
标题:c语言变量莫名其妙的被清零是怎么回事?
收藏  订阅  推荐  打印 
yang4055
Rank: 1
等级:新手上路
帖子:8
积分:196
注册:2008-8-18
c语言变量莫名其妙的被清零是怎么回事?

我在外部文件中用一个指针指向了一个内存,并赋值,但在主函数中调用时却莫名地被清零了,这是怎么回事?
搜索更多相关主题的帖子: c语言  变量  内存  赋值  指针  
2008-8-18 17:14
爱喝牛奶的猫咪
Rank: 2
来自:QQ群46520219
等级:ID已被封
帖子:514
积分:5824
注册:2008-6-16

我也不知道你想说什么回事


[color=white]<>
2008-8-18 17:18
s54661559
Rank: 1
来自:廣州
等级:新手上路
帖子:4
积分:140
注册:2008-8-16

LZ说得好深奥喔
2008-8-18 17:21
canfaint
Rank: 1
等级:新手上路
帖子:6
积分:160
注册:2008-8-10

楼主..具体点行么?
或者把代码贴出来也好啊..
2008-8-19 12:14
hundnn
Rank: 2
等级:注册会员
帖子:107
积分:1232
注册:2007-8-23

外部变量在定义时如果没有赋值,默认值为0.跟局部变量不一样/.


我只能告诉你这些,
因为我没看懂你在说什么

人生若只如初见/何事秋风悲画扇
2008-8-19 16:16
weijincheng
Rank: 1
等级:新手上路
帖子:2
积分:120
注册:2008-9-1
c语言变量莫名其妙被改变

#include<stdio.h>
#include<math.h>

int AADT=1992;   /*AADT*/
double avaxle=2.54;  /*axle number per truck*/
double lane=0.5;           /*lane coefficient*/
double direction=0.7;      /*direction coefficient*/

double PSIGLE=57.1;
double PTRADEM=34.5;
double PTRIDEM=8.4;  /*Traffic composition*/



double distribution[100];

int section;

void main()
{
int i,j,k,l,month,day,hour;
double temperature=0.0;  /* for read the everyday temperature*/

double yeartraffic; /* total year traffic*/

double ptemperature;
double ns,nr;

double h; /* pavement thickness*/
double z; /* half pavement thickness*/
double modulus,strain;
double c=0.435373; /* fatigue function*/

double d,damage; /*cumulative damage*/
double n,nn; /* Miner hypothisis*/
double monthadjust[12]; /*for read the month traffic adjust coefficient*/
double houradjust[24];  /*for read the day traffic adjust coefficient */

double singleweight[50],singlepercent[50];
double trademweight[50],tradempercent[50];
double tridemweight[50],tridempercent[50];

int days; /*month days*/

int SN=0,TAN=0,TIN=0;


FILE *ft;
FILE *fp;
FILE *fs;
FILE *fr;

printf("Please input pavement structure number(1,2,3,4,5):\n");
scanf("%d",&section);
if(section==1) h=50;
if(section==2) h=38;
if(section==3) h=38;
if(section==4) h=33;
if(section==5) h=15;
z=h/2;
yeartraffic=AADT*avaxle*365*lane*direction; /*year total traffic*/

fs=fopen("month.txt","r");
if(fs==NULL)printf("file open error!\n");
while(!feof(fs))
{fscanf(fs,"%d %lf\n",&i,&ns);
  monthadjust[i]=ns;
  printf("month=%d,coefficient=%f\n",i,monthadjust[i]);
  }
  fclose(fs);
  system("pause"); /* to read the month traffic adjust coefficient*/

printf("month=%f\n",monthadjust[12]);system("pause");
fs=fopen("hour.txt","r");
if(fs==NULL) printf("file open errors!\n");
while(!feof(fs))
  {fscanf(fs,"%d %lf\n",&i,&ns);
   houradjust[i]=ns;
   printf("hour=%d,coefficient=%f\n",i,houradjust[i]);
   }
   fclose(fs);
   system("pause"); /*to read the day traffic adjust coefficient*/
   printf("month=%f\n",monthadjust[12]);system("pause");
   for(i=1;i<=12;i++)
   printf("%f\n",monthadjust[i]);

}
monthadjust[i]变量当i=12时被没名奇妙的变为houradjust[i]一样的数是怎么回事?
c中的变量还会冲突或自动改变吗?
2008-9-1 09:02
weijincheng
Rank: 1
等级:新手上路
帖子:2
积分:120
注册:2008-9-1
求助

c语言变量莫名其妙被改变
#include<stdio.h>
#include<math.h>

int AADT=1992;   /*AADT*/
double avaxle=2.54;  /*axle number per truck*/
double lane=0.5;           /*lane coefficient*/
double direction=0.7;      /*direction coefficient*/

double PSIGLE=57.1;
double PTRADEM=34.5;
double PTRIDEM=8.4;  /*Traffic composition*/



double distribution[100];

int section;

void main()
{
int i,j,k,l,month,day,hour;
double temperature=0.0;  /* for read the everyday temperature*/

double yeartraffic; /* total year traffic*/

double ptemperature;
double ns,nr;

double h; /* pavement thickness*/
double z; /* half pavement thickness*/
double modulus,strain;
double c=0.435373; /* fatigue function*/

double d,damage; /*cumulative damage*/
double n,nn; /* Miner hypothisis*/
double monthadjust[12]; /*for read the month traffic adjust coefficient*/
double houradjust[24];  /*for read the day traffic adjust coefficient */

double singleweight[50],singlepercent[50];
double trademweight[50],tradempercent[50];
double tridemweight[50],tridempercent[50];

int days; /*month days*/

int SN=0,TAN=0,TIN=0;


FILE *ft;
FILE *fp;
FILE *fs;
FILE *fr;

printf("Please input pavement structure number(1,2,3,4,5):\n");
scanf("%d",&section);
if(section==1) h=50;
if(section==2) h=38;
if(section==3) h=38;
if(section==4) h=33;
if(section==5) h=15;
z=h/2;
yeartraffic=AADT*avaxle*365*lane*direction; /*year total traffic*/

fs=fopen("month.txt","r");
if(fs==NULL)printf("file open error!\n");
while(!feof(fs))
{fscanf(fs,"%d %lf\n",&i,&ns);
  monthadjust[i]=ns;
  printf("month=%d,coefficient=%f\n",i,monthadjust[i]);
  }
  fclose(fs);
  system("pause"); /* to read the month traffic adjust coefficient*/

printf("month=%f\n",monthadjust[12]);system("pause");
fs=fopen("hour.txt","r");
if(fs==NULL) printf("file open errors!\n");
while(!feof(fs))
  {fscanf(fs,"%d %lf\n",&i,&ns);
   houradjust[i]=ns;
   printf("hour=%d,coefficient=%f\n",i,houradjust[i]);
   }
   fclose(fs);
   system("pause"); /*to read the day traffic adjust coefficient*/
   printf("month=%f\n",monthadjust[12]);system("pause");
   for(i=1;i<=12;i++)
   printf("%f\n",monthadjust[i]);

}
monthadjust[i]变量当i=12时被没名奇妙的变为houradjust[i]一样的数是怎么回事?
c中的变量还会冲突或自动改变吗?
2008-9-1 09:03
StarWing83
Rank: 12Rank: 12Rank: 12
来自:湖北工业大学
等级:版主
威望:9
帖子:2483
积分:26219
注册:2007-11-16

printf("month=%f\n",monthadjust[12]);

monthajust只有0~11这12个元素,你引用12干嘛?

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-9-1 10:54
qfyzy
Rank: 3Rank: 3
来自:安徽合肥
等级:中级会员
威望:1
帖子:344
积分:3728
注册:2008-2-17

0-11为合法下标。12越界,至于为什么会和别的一样,与数据存储的细节有关。

当对C的经验增加时,它会显的很好用.----Dennis M Ritche如是说
2008-9-1 12:35
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.059779 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved