我想将来自用户的一个输入分配给两个不同的指针(我想将其读取为浮点数和字符)。任何人都对如何做到这一点或从一个转换到另一个的简单方法有任何想法?
问问题
110 次
2 回答
1
char inputString[MAX_SIZE]; // Your input will be stored here as a string.
float inputFloat; // Here's where you will have your input as float
float *inputFloatPointer;
inputFloatPointer = &inputFloat; // Do this if you want 2 pointers, as requested
fgets(inputString, MAX_SIZE, stdin); // Read from your input buffer
if ((sscanf(inputString, "%f", inputFloatPointer)) == 1) // Try to parse it as a float
printf("You read a float.\n"); // If the parsing succeeds, you have your float
else
printf("This is no float.\n"); // Else the user typed something that's not a float.
于 2013-11-05T19:42:54.880 回答
0
一个 char 有一个整数表示,而不是一个浮点数。您必须将浮点数修改为 char 表示形式,然后将其转换为 char。
于 2013-11-05T19:42:53.217 回答