簡體   English   中英

錯誤:從較小的 integer 類型“int”轉換為“string”(又名“char *”)[-Werror,-Wint-to-pointer-cast]

[英]error: cast to 'string' (aka 'char *') from smaller integer type 'int' [-Werror,-Wint-to-pointer-cast]

我在解決 CS50 的凱撒問題時遇到了這個錯誤。 這個錯誤是什么意思?

我期待打印密鑰(從字符串類型轉換為 int)幫助我得到答案......

#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>

bool isnotdigit(string inp_key);
int main(int argc, string argv[])
{

   int key;
   if (argc > 2 || isnotdigit(argv[1]))
   {
    printf("Usage: ./caesar key\n");
   }
   else{
    key = (int) argv[1];
    printf("%i\n", key);
   }
}
bool isnotdigit(string inp_key)
{
  
 int uac = 0;
    for (int k = 0; k != strlen(inp_key); k++)
    {
        if (isalpha(inp_key[k]) || isblank(inp_key[k]) || isspace(inp_key[k]))
        {
            uac++;
        }
        else
        {
            uac += 0;
        }
    }

   if (uac > 0)
   {
    return true;
   }
   else
   {
    return false;
   }

}


錯誤來自這一行:

key = (int) argv[1];

嘗試改為:

key = atoi(argv[1]);

編輯 1:作為旁注,您的isnotdigit function 在 for 循環k != strlen(inp_key)中有點錯誤,如果檢測到字符,您可以通過退出 for 循環在沒有uac變量的 function 中獲得相同的結果那不是一個數字。

暫無
暫無

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

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