簡體   English   中英

C:如何將指針(數組元素)轉換為int?

[英]C: How to convert a pointer (array element) to int?

我正在嘗試使用fgets進行編碼,該程序會從用戶那里獲得一個數字輸入,然后將其除以10000000。而且我知道一個字符串元素(of type 'int *')不能被一個int類型所除。

int count;
int input[MAXLEN];

for ( count = 1; count <= 20; count++ ) {
      while (( fgets ( input[count], MAXLEN, stdin)) != 0 ) {
        if ( (input[count] / 10000000) != 20 ) {
            /* Some code dealing with this condition */
        }
      }
}

編輯:編譯器給我這個錯誤:

program.c:10:24: error: invalid operands to binary / (have int * and int)

如果輸入必須是char *類型,為什么我的編譯器會說int *

我知道input[count]是一個指針,因此無法進行除法。 誰能告訴我如何解決此問題,或者至少如何將輸入從fgets轉換為int 非常感謝!

input不應為int * ,而應為char * 這將返回一個C字符串,該字符串是一系列以null結尾的字符。 如果使用atoi() ,則可以將字符串的內容解釋為整數。

暫無
暫無

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

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