我是c编程新手,很难搞清楚如何从结构数组创建字符串。我有一堆数据点,我想在程序中。我创建了一个结构数组,现在我需要他们从中创建一个字符串。这是我目前掌握的密码。
#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}};
现在,我想创建一个名为c_string的字符串,并能够使用此函数:
sscanf(c_str,”%d%d%lf%lf%lf%lf“,&n,&m,&gnm,&hnm,&dgnm,&dhnm);
并使用数据点列表进行计算。
谢谢你
最佳答案
您可能希望使用snprintf()
生成格式化字符串,并使用malloc()
创建要写入的字符数组。请注意,您可能需要仔细考虑您需要的字符数组的大小。