C为struct定义main内部数组的大小

时间:2015-06-11 03:36:25

标签: c initialization malloc global sizeof

我看了5个帖子,但仍然无法判断这是否可能......

LA

如您所见,我需要list_of_spheres全局,但在读取文件之前我不知道大小,那么如何初始化此数组呢?

typedef struct {
  long double xc;
  long double yc;
  long double zc;
  long double radio;
  long double Kd;
  long double Ka;
  long double Ks; // specular
  color fc; 
} SPHERE;

// *array_of_spheres;  // Original line omitted SPHERE

SPHERE *array_of_spheres;

int main(int argc, char** argv) {

  int number_spheres = read_file(); 
//this return the number of spheres in the text file after fscanf

  array_of_spheres = malloc(sizeof(SPHERE)*number_spheres);
  .
  .
  .
  .
}

我尝试了error: expected primary-expression before ‘)’ token array_of_spheres = malloc(sizeof(SPHERE)*number_spheres); sizeof(SPHERE)

原始代码是西班牙语 在.h

sizeof(*SPHERE)
在读取文件并计算球体数量后,在.c中

typedef struct 
{
    long double xc;
    long double yc;
    long double zc;
    long double radio;
    long double Kd;
    long double Ka;
    long double Ks; // especular
    color fc;   
}esfera;

esfera *lista_esferas;

RayT.c:29:40:错误:')'令牌之前的预期primary-expression   lista_esferas = malloc(sizeof(* esfera)* cantidad_de_esferas);

1 个答案:

答案 0 :(得分:2)

array_of_spheres的全局变量声明错误,应如下所示

SPHERE *array_of_spheres;