LoveUnix » 编程开发 & Rational » 新手上路,各位大大救我一救!
让LU留住您的每

一天 让LU博客留住您的每一天
2007-10-18 12:07 wind0520
新手上路,各位大大救我一救!

在unix下,编译通过了,但执行时报 Memory fault(coredump) 错,各位仁兄帮我看一下:
#include <stdio.h>
main()
{   
int x,y,z=1234;
  int *point,*point1,*point2;
  int size;
  FILE *fp;
  struct Fstruct {
   int FirstVar;
   char SecondVar[10];
   } Fstruct[1],Sstruct[1];
  y=15;x=9;
  point=&z;
  point1 = &x;
  point2 = &y;
  Fstruct[0].FirstVar=101;
  strcpy(Fstruct[0].SecondVar, "ABCDEFGHGH");
  Fstruct[1].FirstVar=202;
  strcpy(Fstruct[1].SecondVar, "HIJKLMNNMN");
  fp = fopen("a.txt","w+b");
  size=fwrite(Fstruct, sizeof(Fstruct), 2, fp);
  fclose(fp);
  printf("size=[%d]\n\n",size);
  fp=fopen("a.txt","rb");
  fread(Sstruct,sizeof(Sstruct), 2, fp);
  printf("Sstruct[0].SecondVar=%s\n\n",Sstruct[0].SecondVar);
  fclose(fp);
  max(x,y,point,point1,point2);
  printf("*point=[%d]\n",*point);
}
int max(int x,int y,int *point,int *point1,int *point2)
{

  if(x<y)
        *point = *point1;
  else *point = *point2;
  return 0;
}

2007-10-18 12:59 aix9030
用gcc编译通过,执行没问题。

2007-11-2 10:45 Tristing
把你的SecondVar长度声明为11,或者更大,在某些UNIX系统需要给\0留一个位置

2007-11-2 10:49 Tristing
给楼主一个建议,代码风格需要强化。

页: [1]


Powered by Discuz! Archiver 5.5.0  © 2001-2006 Comsenz Inc.