0

我整个早上都在寻找如何做到这一点,但我找不到我要找的东西。我应该提到我对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 和包含函数操作 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);
}
4

2 回答 2

1

您需要为as中定义struct basic_gal*的声明转发声明。但是,将需要对的定义可用,因此您应该将其定义移出到单独的模块中:bin_NGP()struct basic_galmain.cstruct basic_galbin_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; }

asNGAL是一个int*并且NGAL_MAX是一个int. 取消引用NGAL

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

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

gal[*NGAL].ra = ra*pi/180.;
于 2012-10-09T14:15:38.710 回答
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);      
}      
于 2012-10-09T14:19:33.060 回答