gpt4 book ai didi

c - 隐式转换会丢失整数精度

转载 作者:太空宇宙 更新时间:2023-11-04 02:37:36 25 4
gpt4 key购买 nike

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

    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;
}

最佳答案

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

有关详细信息,请参阅:https://stackoverflow.com/a/22184543/248756

关于c - 隐式转换会丢失整数精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35413996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com