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

标签 c pointers scanf

我想将用户的一个输入分配给两个不同的指针(我想将其读取为 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/

相关文章:

c++ - 我应该将我的程序从 C 更新到现代 C 还是 C++?

c - 使用 csh 脚本从文件中读取多行

c++ - 指针的常见用途?

数组的 C++ scanf/printf

c - 如何使用 EOF 结束 scanf 循环?

c - 使用 C 例程和 openssl dgst、rsautl 命令时的不同签名

在C中复制字符串跳过前导字符

c++ - 为什么这个结构没有在函数中设置

c++ - 引用/指针失效到底是什么?

c - 如何使用 scanf 获取我想要的字符串?