繁体   English   中英

C定义main内部结构的数组大小

[英]C Define size of array inside main for a struct

我看了5个帖子,仍然不知道这是否可行。

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);
  .
  .
  .
  .
}

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

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

我尝试了sizeof(SPHERE)sizeof(*SPHERE)

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

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;

在我读取文件并计算球数后在.c中:

lista_esferas = malloc(sizeof( *esfera)*cantidad_de_esferas);

                               ^

RayT.c:29:40:错误:')'令牌list之前预期的主要表达式= aalloc(sizeof(* esfera)* cantidad_de_esferas);

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

SPHERE *array_of_spheres;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM