officerkrupke
officerkrupke

Reputation: 43

create a string from a structure array in c

I'm new to c programming and I'm having a tough time figuring out how to create a string from a structure array. I have a bunch of data points that I want to have in the program. I created a structure array and now I need them to create a string from it. here is the code I have so far.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main()
{ 
 int i=0;


      struct wmm
   {
    float n;
    float m;
    float gnm;
    float hnm;
    float dgnm;
    float dhnm;
   }  book[3]= {{1, 0,  -29496.6,       0.0,       11.6,       0.0},
  {1, 1,   -1586.3,    4944.4,       16.5,     -25.9},
  {2, 0,   -2396.6,       0.0,      -12.1,       0.0},
  {2, 1,    3026.1,   -2707.7,       -4.4,     -22.5}};

Now I would like to create a string called c_string and be able to use this function:

sscanf(c_str,"%d%d%lf%lf%lf%lf",&n,&m,&gnm,&hnm,&dgnm,&dhnm);

and use the list of data points for computations.

Thank you

Upvotes: 0

Views: 275

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272497

You probably want to use snprintf() to do the formatted string generation, and malloc() to create a character array to write into. Note that you may need to think carefully about large you need your character array to be.

Upvotes: 3

Related Questions