1

I'm getting an error that says "implicit conversion loses integer precision" any suggestions on why it's happening on the line

    maxLen = strlen(line);

Here's the code:

     #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;
    }
Michiel Pater
  • 22,377
  • 5
  • 43
  • 57
Gavin Coll
  • 89
  • 1
  • 1
  • 11

2 Answers2

2

strlen() returns a size_t, which is an unsigned integer.

For more information, see: https://stackoverflow.com/a/22184543/248756

Community
  • 1
  • 1
Michiel Pater
  • 22,377
  • 5
  • 43
  • 57
  • 1
    `size_t` is not just "equivalent", but it **is** an unsigned integer. If you have `unsigned int` in mind: _That_ is wrong! And the second sentence is wrong, too. A positive `int` can be represented in an `unsigned int`. – too honest for this site Feb 15 '16 at 16:15
1

size_t and int represent different ranges.

size_t, the result type of the sizeof operator, can express any index of an array. int may not have that range, or rarely, it may excessively encode that range. strlen() returns size_t, It is the best type, that is not excessively wide, to sufficiently represent all legal results. It is an unsigned integer type. (Not necessarily the type unsigned.)

So if SIZE_MAX > INT_MAX, as it appears to be on OP's platform, some lost of information can be lost when converting from size_t to int - hence the warning.

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

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

Better to use

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

BTW your have at least 2 more problems

       // 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';
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256