开发者

assignment makes integer from pointer without a cast

开发者 https://www.devze.com 2023-03-08 15:01 出处:网络
In the below code am getting an error while am assigning len1 to array a The error is assignment makes integer from pointer without a cast

In the below code am getting an error while am assigning len1 to array a The error is

assignment makes integer from pointer without a cast

Please help me.

void main()
{
    int i=0;
    char a[7]={0x00,0xdc,0x01,0x04};
    char str[70] = "12345664445446464445645645656456454开发者_StackOverflow4132131113132132";
    int len=0;
    char len1[50]={};
    len = strlen(str);
    printf("The Length is : %02x", len);
    printf("The Length is : %d", len);
    sprintf(len1,"0x%02x",len);
    printf("The Len1 value : %s", len1);

    a[4]=len1; // This line causes the error.

    for(i=0;i<=7;i++)
    {   
        printf("%05c",a[i]);
        //printf("%x",a[i]);
    }
}


What are the types of the (sub-)expressions in your statement?

a[4] = len1;

len1 is an array of 50 chars (type char[50]) but in the expression above it decays to a pointer to its first element. That's a value of type char*.
a[4] is an object of type char.

You cannot assign a char* to an object of type char!

I think maybe you wanted

/* BEWARE BUFFER OVERFLOWS */
strcpy(a+4, len1); /* a+4 is of type `char*`, len1 decays to `char*` */


The problem is that a[4] is a char, whereas len1 is an array of chars!

When you declare

char a[7] = {...}

You're saying that you want a to be an array of chars and you're initializing the values at a to the values in {...}.

When you say a[4] you're saying go 4 along from where a is and use the value there, which is a char. But len1 is a char pointer so when you assign it to a[4] you get the warning!

0

精彩评论

暂无评论...
验证码 换一张
取 消