我如何将浮点值分配给 char* c[] 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11841782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:37:02  来源:igfitidea点击:

how can i assign float value to char* c[] array

c++c

提问by poppy

I have string,int,float values and trying to assign those values to char* c[] array like this.

我有 string、int、float 值并尝试将这些值分配给 char* c[] 数组,如下所示。

char *str = "helloo";
int int = 1000;
short st1[]={32760};
float flt = 2.345; 
char*  c [] = {(char*)int1,(char*)str,(char*)flt,(char*)st1};

but for float getting illegal explicit conversion from 'float' to 'char * 'anybody tel me how to assign?

但是对于 float从“float”到“char *”的非法显式转换,有人告诉我如何分配吗?

回答by Kerrek SB

In C++:

在 C++ 中:

#include <string>

std::string arr[] = { std::to_string(1000),
                      "helloo",
                      std::to_string(2.345f),
                      std::to_string(32760)    };

You can use arr[0].c_str()etc. to get back char const *s.

您可以使用arr[0].c_str()etc. 来取回char const *s。

回答by paxdiablo

You can't just castthose types and expect to get useful results. Casting is a minimal operation which can work to transform data in certain predefined ways, but it won't (for example) intelligently turn integers into their string representations.

您不能只是强制转换这些类型并期望获得有用的结果。转换是一种最小的操作,它可以以某些预定义的方式转换数据,但它不会(例如)智能地将整数转换为它们的字符串表示形式。

That's why your compiler is complaining.

这就是你的编译器抱怨的原因。

If you want the string representation of numeric data, you have to convert them differently, with something like:

如果您想要数字数据的字符串表示形式,则必须以不同的方式转换它们,例如:

char intStr[30]
sprintf (intStr, "%d", int1);

If you're looking to get string representations of them all, you can do something like:

如果您想获得它们的所有字符串表示,您可以执行以下操作:

char *str = "helloo";
int int1 = 1000;
short st1[]={32760};
float flt = 2.345; 

char mybuff1[50], mybuff2[50], mybuff3[50], mybuff4[50];
sprintf (mybuff1, "%d", int1);
sprintf (mybuff2, "%s", str);
sprintf (mybuff3, "%f", flt);
sprintf (mybuff4, "%d", st1[0]);

char *c [] = {mybuff1, mybuff2, mybuff3, mybuff4);


And be aware that, although C and C++ have very similar idioms, and are mostly compatible if you stick to a subset, they are notthe same language and the best way to do something changes dramatically depending on the actual language you're using.

并且请注意,尽管 C 和 C++ 具有非常相似的习语,并且如果您坚持使用一个子集,它们大多是兼容的,但它们不是同一种语言,并且根据您使用的实际语言,执行某事的最佳方法会发生巨大变化。

For example, it's rarely necessary to use C-style strings in C++ since that language provides an impressive real string type. Ditto for malloc/freeas opposed to new/deleteand many other aspects.

例如,很少需要在 C++ 中使用 C 风格的字符串,因为该语言提供了令人印象深刻的真实字符串类型。同上,malloc/free而不是new/delete许多其他方面。

Questions should generally be tagged C or C++, rarely both.

问题通常应该标记为 C 或 C++,很少同时标记。