Global vector with variable size - C programming

Asked

Viewed 58 times

1

I’m trying to create a global vector whose size is defined within a function. My code got this way:

struct body{
    double m;
    double a;
    double n;
    double e;
    double w;
    double I;
    double W;
};

struct body *planet_global;
struct body *particle;

int qtd_planet = 0, qtd_tp=0;

void entrada_de_dados(){
    int pi=0;

//------ENTRADA DE DADOS DOS PLANETAS----------------------------------------------------------------------------------------------------------------------------------------------

    char url[]="entrada/planets.txt";
    FILE *arq;
    pi=0;


    arq = fopen(url, "r");
    char ch;

    if(arq == NULL)
        printf("Erro, nao foi possivel abrir o arquivo\n");

    while( (ch=fgetc(arq))!= EOF ){

        if(ch == '\n'){
            printf("%c",ch);
            qtd_planet++;
        }         
    }

    rewind(arq) ; 

    printf("\n qtd_planet = %d\n\n",qtd_planet);

    struct body planet_local[qtd_planet+1];                                         //qtd + 1 pq a ultima linha nao possui "\n"
    planet_global = (struct body *)malloc(qtd_planet+1 * sizeof(struct body));

    while( (fscanf(arq,"%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", &planet_local[pi].m, &planet_local[pi].a, &planet_local[pi].n, &planet_local[pi].e, &planet_local[pi].w, &planet_local[pi].I, &planet_local[pi].W))!=EOF ){
        printf("%d\t%e\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n\n",pi, planet_local[pi].m, planet_local[pi].a, planet_local[pi].n, planet_local[pi].e, planet_local[pi].w, planet_local[pi].I, planet_local[pi].W);
        pi++;
    }
    planet_global   =   planet_local;

    fclose(arq);
//------ENTRADA DE DADOS DA PARTICULA----------------------------------------------------------------------------------------------------------------------------------------------

    char url_p[]="entrada/test_particle.txt";
    FILE *arq_tp;
    arq_tp = fopen(url_p, "r");
    pi=0;

    double G    =   6.672e-11*pow(1/1.495978707e11,3)/(3.171e-8*3.171e-8);      //[AU^3 kg^-1 year^-2]
    double Msun =   1.98911e30;

    char ch_tp;

    if(arq_tp == NULL)
        printf("Erro, nao foi possivel abrir o arquivo\n");

    while( (ch=fgetc(arq_tp))!= EOF )
        printf("%c",ch);
        if(ch_tp == '\n')
          qtd_tp++;
    rewind(arq_tp)  ; 
    printf("\n qtd_tp = %d\n\n",qtd_tp);

    struct body particles_local[qtd_tp+2];
    particle = (struct body *)malloc(qtd_tp +2 * sizeof(struct body));


    arq_tp = fopen(url_p, "r");
    if(arq_tp == NULL)
            printf("Erro, nao foi possivel abrir o arquivo\n");
    else{   //#semi-major           free            free        free longitude      free ascending
            //#  axis   [AU]    eccentricity    inclination[°]  of pericentre           node
        while((fscanf(arq_tp,"%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", &particles_local[pi].a, &particles_local[pi].e, &particles_local[pi].w, &particles_local[pi].I, &particles_local[pi].W)) != EOF){
            particles_local[qtd_tp].n = G*Msun*(planet_global[0].m + planet_global[1].m)/(pow(particles_local[qtd_tp].a,3)) ;       
            printf("%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n\n", particles_local[pi].a, particles_local[pi].n, particles_local[pi].e, particles_local[pi].w, particles_local[pi].I, particles_local[pi].W);
            pi++;
        }

        particle =  particles_local;
    }

    static struct body particles_local_static[] = particle;

    fclose(arq_tp); 
}
int main(int argc, char* argv[]){

    entrada_de_dados();

    struct body particles_local[qtd_tp+2];
    *particles_local    =   *particle;

    printf("\n%d\t%e\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n\n",0, planet_global[0].m, planet_global[0].a, planet_global[0].n, planet_global[0].e, planet_global[0].w, planet_global[0].I, planet_global[0].W);

    printf("%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n\n", particles_local[0].a, particles_local[0].n, particles_local[0].e, particles_local[0].w, particles_local[0].I, particles_local[0].W);
}

The big problem is that the address of the local structure with the values is getting lost and when I try to use it in another function I end up having only garbage. I’m not sure how to guarantee the values I set for this vector. Someone could help me?

  • Try to summarize the code, do not need to put your entire code, create a new source code representing the problem in the simplest way possible

  • 1

    because you have two functions main and two "data input()"?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.