有关main函数中提取结构体中的元素
大家好,新手向各位前辈问好。有一个简单程序,我不知道为什么运行出错,但我基本可以断定是main函数中有关U和L的问题,希望得到广大前辈的帮助。谢谢大家。程序代码:
#include <stdio.h> #include <stdlib.h> #include <math.h> /* FMG Solver of the EHL circular contact */ #define pi 3.1415926535897931 typedef struct { double hx; int ii; double *p, *f; double *hfi, *hrhs; double *w; double *K; double *pjac; /*old pressure for use in Jacobi relaxation*/ double rg; double Hm; } Level; typedef struct { int nx0; int maxlevel; double xa,xb; double h0; Level *Lk; } Stack; void initialize(Stack *U, int nx0, int maxl, double xa, double xb, double h0) { /* initialize values in datastructure */ double hx; Level *L; int l,ii; U->xa=xa; U->xb=xb; U->maxlevel=maxl; U->h0=h0; U->Lk=(Level *)calloc(maxl+1,sizeof(Level)); hx=(xb-xa)/nx0; ii=nx0; for (l=1; l<=maxl; l++) { L=U->Lk+l; L->hx=hx; L->ii=ii; L->p =(double *)calloc(U->maxlevel+1,sizeof(double)); L->w =(double *)calloc(U->maxlevel+1,sizeof(double)); L->f =(double *)calloc(U->maxlevel+1,sizeof(double)); L->pjac =(double *)calloc(U->maxlevel+1,sizeof(double)); L->hfi =(double *)calloc(U->maxlevel+1,sizeof(double)); L->hrhs=(double *)calloc(U->maxlevel+1,sizeof(double)); L->K =(double *)calloc(U->maxlevel+1,sizeof(double)); printf("\n level: %2d ii=%4d, hx=%f",l,ii,hx); hx*=0.5; ii*=2; } } void init_p(Stack *U, int lev) { int i; Level *L; double x; double *p; L =U->Lk+lev; p=L->p; for (i=0; i<=L->ii; i++) { x=U->xa+i*L->hx; p[i]=0.0; if (x*x<1.0) p[i]=sqrt(1.0-x*x); } FILE *fp; fp=fopen("P1.dat","w"); for (i=0; i<=L->ii; i++) { x=U->xa+i*L->hx; fprintf(fp,"%f %f\n",x,L->p[i]); } fprintf (fp, "\n"); fclose(fp); } void relax(Stack *U, int lev) { int i; Level *L; double *p; L =U->Lk+lev; p =L->p; for (i=0; i<=L->ii; i++) p[i]=p[i]+1; } void main() { Stack U; int i; double x; Level *L; initialize(&U,128,1,-4.5,1.5,1); L=U->Lk+1; init_p(&U, 1); relax(&U, 1); FILE *fp2; fp2=fopen("P2.dat","w"); for (i=0; i<=L->ii; i++) { x=U->xa+i*L->hx; fprintf(fp2,"%f %f\n",x,L->p[i]); } fprintf (fp2, "\n"); fclose(fp2); }