0

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

3
  • 1
    In your example the data is already in the array, so why use sscanf? Commented May 20, 2011 at 15:30
  • I'm modifying a code that imported the data from a separate file but I need to have all of the data self contained in the program. In order to make as few alterations as possible I just wanted to produce a string that was exactly like the string that the program created from the data file. Commented May 20, 2011 at 15:55
  • You are trying to do code generation? If so, don't feel that you have to use c just because the code being generated is c. Other languages might be easier. Commented May 20, 2011 at 16:52

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.