【问题标题】:Implicit conversion loses integer precision隐式转换失去整数精度
【发布时间】:2016-02-15 16:12:03
【问题描述】:

我收到一条错误消息,上面写着“隐式转换失去整数精度”关于为什么会发生这种情况的任何建议

    maxLen = strlen(line);

代码如下:

     #include <stdio.h>
     #include <stdlib.h>
     #include <string.h>
     #include <time.h>

    // Accepts: command line input
    // Returns: 0 if no error

    int main(int argc, char *argv[] )
    {
        int x = 0, i, lCount = 0, maxLen = 0;
        char line[500], *temp;

        FILE *file = fopen("playlist.txt", "r" );

        //  check if file exists
        if (file == NULL){
            printf("Cannot open file\n");
            return 1;
        }

        /* The following code identifies each line in the text and lines are shuffled accordingly */

        while (fgets(line, sizeof(line), file) != NULL)
        {
            lCount++;
            if (strlen(line) > maxLen)
                maxLen = strlen(line);
        }

        rewind(file);
        char *lArray[lCount];

        while (fgets(line, sizeof(line), file) != NULL)
        {
            lArray[x] = malloc(strlen(line));

            if (lArray[x] == NULL){
                printf("A memory error occurred.\n");
                return(1);
            }

            strcpy(lArray[x], line);

            /* change \n to \0 */
            lArray[x][strlen(lArray[x])-1] = '\0';
            x++;
        }

        printf("Your playlist %s has %d lines with %d characters\n", argv[1], lCount, maxLen);

        printf("The original inputted array is:\n");

        for (x = 0; x < lCount; x++)
            printf("%2d %s\n", x, lArray[x]);
        /*  The array will now be shuffled: */

        srand( (unsigned int) time(NULL));

        for (x = lCount - 1; x >= 0; x--){
            i = (int) rand() % lCount;
            temp = lArray[x];
            lArray[x] = lArray[i];
            lArray[i] = temp;
        }

        printf("\nShuffled Array\n");

        for (x = 0; x < lCount; x++)
            printf("%2d %s\n", x, lArray[x]);

        /* Memory will now be freed */
          /* for (x = 0; x < lCount; x++)
            free(lArray[x]);
        strdup(lArray);
        fclose(file); */
        return 0;
    }

【问题讨论】:

  • 这是一团糟!格式化和缩进你的代码!
  • 请阅读如何提供minimal reproducible example。您的大部分代码与问题完全无关,浪费了大家的时间。

标签: c compiler-errors


【解决方案1】:

strlen() 返回一个size_t,它是一个无符号整数。

欲了解更多信息,请参阅:https://stackoverflow.com/a/22184543/248756

【讨论】:

  • size_t 不仅仅是“等价的”,它一个无符号整数。如果您想到了unsigned int 是错误的!第二句话也是错误的。积极的int 可以用unsigned int 表示。
【解决方案2】:

size_tint 代表不同的范围。

size_tsizeof 运算符的结果类型,可以表示数组的任意索引。 int 可能没有该范围,或者很少会过度编码该范围。 strlen() 返回size_t,它是最好的类型,不会过宽,足以充分代表所有合法结果。它是无符号整数类型。 (不一定是unsigned类型。)

因此,如果 SIZE_MAX &gt; INT_MAX 似乎在 OP 的平台上,则在从 size_t 转换为 int 时可能会丢失一些信息 - 因此会出现警告。

#include <string.h>
// size_t strlen(const char *s);

int maxLen;
maxLen = strlen(line);  // warning

更好用

size_t maxLen = strlen(line);
...
//                           note prefix ---v
printf("Your playlist %s has %d lines with %zu characters\n",
   argv[1], lCount, maxLen);

顺便说一句,您至少还有 2 个问题

       // insufficient space 
       // lArray[x] = malloc(strlen(line));
       lArray[x] = malloc(strlen(line) + 1);
       ...
       strcpy(lArray[x], line);

       // Following is a hacker's delight as strlen(lArray[x]) may return 0
       /* change \n to \0 */
       // lArray[x][strlen(lArray[x])-1] = '\0';

       // Instead simplest to lop \n from `line`
       while (fgets(line, sizeof(line), file) != NULL) {
         line[strcspn(line, "\n")] = '\0';

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 2017-03-02
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多