Scanf将一个变量分配给C中的两个指针

时间:2013-11-05 19:36:37

标签: c pointers scanf

我想将一个用户输入分配给两个不同的指针(我想把它读作float和char)。任何人都有关于如何做到这一点的想法或一个简单的方法从一个转换到另一个?

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

好的char有一个整数表示,而不是float。您必须将 MODIFY 浮点数转换为char表示形式,然后将其转换为char。