Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C sprintf():仅扫描浮点数组中的第一个值_C - Fatal编程技术网

C sprintf():仅扫描浮点数组中的第一个值

C sprintf():仅扫描浮点数组中的第一个值,c,C,我可以把浮点数组转换成字符串吗?我不在乎点是否是字符串值。我只需要把数字分成字符串 到目前为止,我已经做到了这一点 void H(float *suma, int k){ int i=0; char str[200] = ""; sprintf(str, "%.2f", *suma); for(i=0;i<strlen(suma);i++) { printf("%c", str[i]); } } 但我也需要它 string[0]='1'; string[1

我可以把浮点数组转换成字符串吗?我不在乎点是否是字符串值。我只需要把数字分成字符串

到目前为止,我已经做到了这一点

void H(float *suma, int k){
int i=0;
char str[200]  = "";

    sprintf(str, "%.2f", *suma);


for(i=0;i<strlen(suma);i++) {
    printf("%c", str[i]);
}
}
但我也需要它

  string[0]='1';
  string[1]='2';
  string[2]='3';
  string[3]='.';
  string[4]='4';
  string[5]='5';
  string[6]='5';
  string[7]='4';
等等。

这里有一个解决方案:

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

void H(float *suma, int k)
{
    int i,j;
    char str[200]  = "";
    for(j=0; j<k; j++)
    {
        sprintf(str, "%.2f", suma[j]);

        for(i=0;i<strlen(str);i++)
        {
            printf("%c", str[i]);
        }
        printf("\n");
    }
}

int main ()
{
    float b[]={123.432,213.432,12.2,31.3,13.4};
    H(b,5);
    system("pause");
    return 0;
}
#包括
#包括
#包括
空位H(浮动*suma,整数k)
{
int i,j;
字符str[200]=“”;

对于(j=0;j如果要以字符串格式打印所有数组编号的列表,这里是解决方案(我假设k是浮点数组的长度):

void H(浮点*suma,整数k){
int i=0,j;
字符str[200]=“”;

对于(j=0;j@hmjd我只是想打印那个字符串。我认为它给了我数组中的确切字符数。但我想我错了,否则你不会问。当我的suma数组中有5个浮点数时,这行得通吗?你真聪明!这正是我需要的!很高兴知道我得到了你的帮助。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void H(float *suma, int k)
{
    int i,j;
    char str[200]  = "";
    for(j=0; j<k; j++)
    {
        sprintf(str, "%.2f", suma[j]);

        for(i=0;i<strlen(str);i++)
        {
            printf("%c", str[i]);
        }
        printf("\n");
    }
}

int main ()
{
    float b[]={123.432,213.432,12.2,31.3,13.4};
    H(b,5);
    system("pause");
    return 0;
}
void H(float *suma, int k){
    int i=0, j;
    char str[200]  = "";

    for(j=0;j<k;j++){
        sprintf(str, "%.2f", *(suma+j));
        for(i=0;str[i]!='\0';i++) {
            printf("%c\n", str[i]);
        }
    }
}