Home > other >  assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]

Time:07-09

I'm trying to keep concatenating the largeVal with smallVal when input a, and store the result into arr[] array.

int driver()
{
    char buffer[MAXLINE];
    char reply[MAXLINE * 1000];
    char largeVal[MAXLINE] =
        "a1b2c3d4e5f6g7h8i9j10a1b2c3d4e5f6g7h8i9j10a1b2c3d4e5f6g7h8i9j10a1b2c3d4e5f6g7h8i9j10a1b2c3d4e5f6g7h8i9j10a1b2c3d4e5f6g7h8i9j10a1b2c3d4e5f6g7h8i9j10a1b2c3d4e5f6g7h8i9j10";
    char smallVal[MAXLINE] = "5";

    while (strcmp(buffer,"a") == 0)
    {
        arr[MAXLINE * 1000] = strcat(smallVal, largeVal);
    }
    return foo(buffer, reply);
}

The error message is the following:

warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]

   91 |         arr[MAXLINE * 1000] = strcat(smallReply, largeReply);
      |                               ^

How should I fix this?

CodePudding user response:

In the line

arr[MAXLINE * 1000] = strcat(smallVal, largeVal);

there are two issues. First, you are not assigning the string to arr, but trying to access the element in position [MAXLINE * 1000], which, in turn is just a plain char. Second, strcat returns an pointer to a char (array), i.e.: char *. So, you are trying to assign a pointer in a place that expects a value.

CodePudding user response:

The compiler message is clear enough. In this assignment statement

arr[MAXLINE * 1000] = strcat(smallVal, largeVal);

the left operand has the type char while the right operand has the type char * (the return type of the function strcat). So the statement does not make a sense.

It seems you mean something like the following

strcpy( arr, strcat(smallVal, largeVal) );

Pay attention to that the array smallVal should be large enough to store the appended string from the array largeVal.

Also this while loop

while (strcmp(buffer,"a") == 0)
{
    arr[MAXLINE * 1000] = strcat(smallVal, largeVal);
}

in any case invokes undefined behavior because the local array buffer is uninitialized.

char buffer[MAXLINE];

And moreover even if the array buffer will be initialized by the string "a" the loop will be infinite.

  • Related