我想将用户的一个输入分配给两个不同的指针(我想将其读取为 float 和字符)。任何人都知道如何执行此操作或从一种转换为另一种的简单方法吗?
最佳答案
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.
关于c - Scanf将一个变量分配给C中的两个指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19797356/