簡體   English   中英

Scanf將一個變量分配給C中的兩個指針

[英]Scanf to assign one variable to two pointers in C

我想將一個用戶的輸入分配給兩個不同的指針(我想將其作為浮點數和char讀取)。 任何人都對如何執行此操作有任何想法,或者有一種簡單的方法可以將它們相互轉換?

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.

一個char具有整數表示形式,而不是浮點數。 您將必須將float 修改為char表示形式,然后將其轉換為char形式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM