c - Returning the address of a structure -


this code working on , in function trace* readtrace(char* filename) have read file (that fills structure) , return address of trace structure. time , value of structure pointers don't know how it.

 #include <stdio.h>  #include <stdlib.h>  #include <conio.h>  #include <math.h>   #define tmax 1000  #define nbpts 2000  #define dt 0.5   typedef struct  {     char comment[40];     int nbpts;     float time[4096];     float value[4096];  } trace;   void simutrace(int tmax, float dt, float params[], trace *unetrace)  {     printf("shkruani emrin e eksperimentit : \n");     scanf("%s", &unetrace->comment);      int = 0;     float v = 0, w = 0, dv = 0, dw = 0, t = 0;     float = params[0], d = params[1], e = params[2];      while (t <= tmax)     {             dv = (a - v) * (v - 1) * v - w;             dw = e * (0.5 * v - w - d);             v += dv * dt;             w += dw * dt;             unetrace->time[i] = t;             unetrace->value[i] = v;             i++;             t += dt;     }     unetrace->nbpts = i;  }   void printtrace(trace unetrace)  {     printf("%s\n", unetrace.comment);     printf("\t%d\n", unetrace.nbpts);     int i;     (i = 0; <= nbpts; i++) {             printf(" t= %.1f \tv= %.4f \n ", unetrace.time[i],unetrace.value[i]);     }  }   void savetracebin(char *filetrace, trace unetrace)  {     file *file;     file = fopen(filetrace, "w");     if (fopen(filetrace, "w") == null) {             printf("\n gabim! \n");     } else {             fprintf(file, "%s\n", unetrace.comment);             fprintf(file, "%d", unetrace.nbpts);             int i;             (i = 0; <= nbpts; i++) {                     fprintf(file, "\n %.1f %.4f",unetrace.time[i],unetrace.value[i]);             }             fclose(file);     }  }   void readtrace(char *filename, trace *unetrace)  {     file*file;     file = fopen(filename, "r");     if (file != null) {             fscanf(file, "%s", unetrace->comment);             fscanf(file, "%d", &unetrace->nbpts);             int i;             (i = 0; <= nbpts; i++) {                     fscanf(file, "%f", &(unetrace->time[i]));                     fscanf(file, "%f", &(unetrace->value[i]));             }             printf("\n leximi perfundoi me sukses!\n");     } else {             printf("\n gabim! \n");     }     fclose(file);  }   trace* readtrace(char* filename) {     file*file;     file = fopen(filename, "r");     if (file != null) {             fscanf(file, "%s", unetrace->comment);             fscanf(file, "%d", &unetrace->nbpts);             int i;             (i = 0; <= nbpts; i++) {                     fscanf(file, "%f", &(unetrace->time[i]));                     fscanf(file, "%f", &(unetrace->value[i]));             }             printf("\n leximi perfundoi me sukses!\n");     } else {             printf("\n gabim! \n");     }     fclose(file);  }   float errortrace(trace unetrace1, trace unetrace2)  {     float sum = 0;     int i;     (i = 0; <= nbpts; i++)     {             sum += (unetrace1.value[i] - unetrace2.value[i]);     }     sum /= nbpts;     return sqrt(fabs(sum));  }    int main()  {     float pa[] = { 0.5, 0.01, 0.05 };     float pb[] = { 0.75, 0.3, 0.1 };      float e1, e2;     trace tracepa, tracepb, tracecell;      simutrace(nbpts, dt, pa, &tracepa);     printtrace(tracepa);     savetracebin("myfile1.txt", tracepa);      simutrace(nbpts, dt, pb, &tracepb);     printtrace(tracepb);     savetracebin("myfile2.txt", tracepb);      readtrace("cell.txt", &tracecell);      e1 = errortrace(tracecell, tracepa);     e2 = errortrace(tracecell, tracepb);      printf("\n gabimi nga llogaritja e pa : %f ", e1);     printf("\n gabimi nga llogaritja e pb : %f ", e2);      if (e1 < e2)             printf("\n\n rasti pa eshte me mire se rasti pb \n");     else             printf("\n\n rasti pb eshte me mire se rasti pa \n");      return  0;  } 

you can either return trace* allocated in readtrace,

trace* readtrace(char* filename) {     trace *tp = malloc(sizeof *tp);     if (!tp) return null;     // fill tp file     .... } // call in main  trace *t = readtrace("cell.txt"); free(t); // free when done 

or supply trace function, like

void readtrace(char* filename, trace *tp) {     if (!tp) return;     // fill tp file     .... } // call in main  trace t; // define trace object readtrace("cell.txt", &t); 

as fill time , value, read array file:

int i; fscanf(file, "%d", &t->nbpts); (i = 0; < t->nbpts; i++) {     fscanf(file, "%f", &(t->time[i]));     fscanf(file, "%f", &(t->value[i])); } 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -