使用指向头文件中声明的函数中的结构的指针

时间:2012-10-09 14:09:47

标签: c++

我一直在寻找如何做到这一点,我找不到我想要的东西。我应该提一下,我对C来说还是个新人。

我正在尝试对我的代码进行模块化(这是有效的,直到我尝试这样做)

目前在我的主要代码中:

#include<stdio.h>
#include<rfftw.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include <fstream>
#include <iomanip>
#include<complex>
#include<omp.h>

struct basic_gal {
   double ra,dec,z,dist,fkp,nbar;
   double cp[3];
   double RSD[3];
};

struct basic_gal *gal; 
int NGAL_MAX =200000;

main() {

  if(!(gal = (struct basic_gal*)malloc(NGAL_MAX*sizeof(struct basic_gal))-1)) 
  printf("memory allocation problem for galaxies\n");

  etc etc..
}

然后我继续读取文件并将各种属性分配给结构。 我想要做的是将空结构(或指向它的指针)传递给函数,填写函数中结构的元素,然后将其返回到我的主程序中再次使用。

这需要3个文件,main.c,头文件,header.h和包含函数actions bin_gals.c的文件。

在头文件中我有

#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <stdio.h>
#include <math.h>
#include <string> 

void bin_NGP(int,int*,struct basic_gal*,int);

在bin_gals.c文件中我有

#include "header.h"
#include <all_the_others>

void bin_NGP(int NGAL_MAX, int *NGAL, basic_gal *gal, int flag) {

/*read in files and add data etc*/

}

在main.c文件中我有

#include "header.h"
#include <all_the_others>

struct basic_gal {
   double ra,dec,z,dist,fkp,nbar;
   double cp[3];
   double RSD[3];
};
struct basic_gal *gal;  
int NGAL_MAX = 200000;

main() {

  if(!(gal = (struct basic_gal*)malloc(NGAL_MAX*sizeof(struct basic_gal))-1)) 
  printf("memory allocation problem for galaxies\n");

  int NGAL =0;
  int *ipNGAL =&NGAL;
  bin_NGP(NRAN_MAX,ipNGAL,gal,1);
}

问题在于我认为我没有正确地传递结构,但无法弄清楚如何做到这一点。

我的错误消息看起来像

bin_gals.c: In function ‘void bin_NGP(int, int*, basic_gal*, int)’:
bin_gals.c:150: error: ISO C++ forbids comparison between pointer and integer
bin_gals.c:151: error: invalid types ‘basic_gal*[int*]’ for array subscript

我不知道如何解决这个问题。 任何帮助将不胜感激!

编辑:bin_gals.c的bin_NGP部分的实际内容是:

void bin_NGP(const char *data, int NGAL_MAX, int *NGAL, double *min_x, double *min_y        ,double *min_z, double *max_x, double *max_y ,double *max_z ,struct basic_gal *gal, int     flag) {

  FILE *fp_rand;
  if((fp_rand=fopen(data,"r"))==NULL) printf("data file %d not opened\n", flag);
  const int bsz=100; char buf[bsz];  
  fgets(buf, bsz, fp_rand);                         //header line
  while((fgets(buf, bsz, fp_rand))!=NULL) { 
  double ra, dec, cz; 
  sscanf(buf,"%lf %lf %lf\n",&ra,&dec,&cz);
  if(++NGAL>NGAL_MAX) { NGAL--; break; }
  gal[NGAL].ra = ra*pi/180.;
      gal[NGAL].dec = dec*pi/180.;
      gal[NGAL].z = cz;
      gal[NGAL].dist = calc_dp(gal[NGAL].z);
      gal[NGAL].cp[0] = (gal[NGAL].dist*cos(gal[NGAL].dec)*cos(gal[NGAL].ra)); 
      gal[NGAL].cp[1] = (gal[NGAL].dist*cos(gal[NGAL].dec)*sin(gal[NGAL].ra));
      gal[NGAL].cp[2] = (gal[NGAL].dist*sin(gal[NGAL].dec));
      if (flag ==1) {
        if (gal[NGAL].cp[0] > max_x) max_x = gal[NGAL].cp[0];
        if (gal[NGAL].cp[1] > max_y) max_y = gal[NGAL].cp[1];
        if (gal[NGAL].cp[2] > max_z) max_z = gal[NGAL].cp[2];

        if (gal[NGAL].cp[0] < min_x) min_x = gal[NGAL].cp[0];
        if (gal[NGAL].cp[1] < min_y) min_y = gal[NGAL].cp[1];
        if (gal[NGAL].cp[2] < min_z) min_z = gal[NGAL].cp[2];
      }         
    }
    fclose(fp_rand);
}

我试图通过删除一些输入参数来简化问题..也许我是在伪装一个错误。

编辑:修复。为清楚起见,我已经包含了固定代码,任何人都有同样的问题。

void bin_NGP(const char *data, int NGAL_MAX, int *NGAL, double *min_x, double *min_y ,double *min_z, double *max_x, double *max_y ,double *max_z ,struct basic_gal *gal, int flag) {

  FILE *fp_rand;
  if((fp_rand=fopen(data,"r"))==NULL) printf("data file %d not opened\n", flag);
  const int bsz=100; char buf[bsz];  
  fgets(buf, bsz, fp_rand);                         //header line
  while((fgets(buf, bsz, fp_rand))!=NULL) { 
  double ra, dec, cz; 
  sscanf(buf,"%lf %lf %lf\n",&ra,&dec,&cz);
  if(++(*NGAL) > NGAL_MAX) { *NGAL--; break; }
  gal[*NGAL].ra = ra*pi/180.;
      gal[*NGAL].dec = dec*pi/180.;
      gal[*NGAL].z = cz;
      gal[*NGAL].dist = calc_dp(gal[*NGAL].z);
      gal[*NGAL].cp[0] = (gal[*NGAL].dist*cos(gal[*NGAL].dec)*cos(gal[*NGAL].ra)); 
      gal[*NGAL].cp[1] = (gal[*NGAL].dist*cos(gal[*NGAL].dec)*sin(gal[*NGAL].ra));
      gal[*NGAL].cp[2] = (gal[*NGAL].dist*sin(gal[*NGAL].dec));
      if (flag ==1) {
        if (gal[*NGAL].cp[0] > *max_x) *max_x = gal[*NGAL].cp[0];
        if (gal[*NGAL].cp[1] > *max_y) *max_y = gal[*NGAL].cp[1];
        if (gal[*NGAL].cp[2] > *max_z) *max_z = gal[*NGAL].cp[2];

        if (gal[*NGAL].cp[0] < *min_x) *min_x = gal[*NGAL].cp[0];
        if (gal[*NGAL].cp[1] < *min_y) *min_y = gal[*NGAL].cp[1];
        if (gal[*NGAL].cp[2] < *min_z) *min_z = gal[*NGAL].cp[2];
      }         
  }
  fclose(fp_rand);
}

2 个答案:

答案 0 :(得分:1)

您需要转发声明struct basic_gal*以声明bin_NGP()struct basic_gal定义为main.c。但是,struct basic_gal的定义需要bin_NGP()的定义,因此您应该将其定义从main.c移到单独的模块中:

/* basic_gal.h */
#ifndef BASIC_GAL_DEFINITION
#define BASIC_GAL_DEFINITION

struct basic_gal {
   double ra,dec,z,dist,fkp,nbar;
   double cp[3];
   double RSD[3];
};

#endif

注意,如果这是C,则需要使用struct basic_gal


编辑后,这是违规行:

if(++NGAL>NGAL_MAX) { NGAL--; break; }

NGALint*NGAL_MAXint。取消引用NGAL

if(++*NGAL > NGAL_MAX) { (*NGAL)--; break; }

用作数组索引时取消引用NGAL

gal[*NGAL].ra = ra*pi/180.;

答案 1 :(得分:0)

您需要使您的结构对main.c和bin_gals.c都可见。将标题bin_gals.h修改为:

 #include <stdlib.h> 
 #include <fstream> 
 #include <iomanip> 
 #include <stdio.h> 
 #include <math.h> 
 #include <string>  

 struct basic_gal { 
    double ra,dec,z,dist,fkp,nbar; 
    double cp[3]; 
    double RSD[3]; 
 }; 

 void bin_NGP(int,int*,struct basic_gal*,int); 
然后

bin_gals.c看起来像

 #include "bin_gals.h"   
 #include <all_the_others>   

 void bin_NGP(int NGAL_MAX, int *NGAL, basic_gal *gal, int flag) {   

 /*read in files and add data etc*/   

 }   

您的主文件如下:

#include "bin_gals.h"      
#include <all_the_others>      

struct basic_gal *gal;        
int NGAL_MAX = 200000;      

main() {      

  if(!(gal = (struct basic_gal*)malloc(NGAL_MAX*sizeof(struct basic_gal))-1))       
  printf("memory allocation problem for galaxies\n");      

  int NGAL =0;      
  int *ipNGAL =&NGAL;      
  bin_NGP(NRAN_MAX,ipNGAL,gal,1);      
}