开发者

Assignment makes integer from pointer without a cast in C

I have a problem with this. Here's the specific part of my C code:

unsigned char *p;
char s[1048];
int m[1048], r[2];
int e = 0, L = 0, mov = 0, ri, i;
for(*p = s; *p; ++p, mov += m[L++])
       m[L] = min(*p - 'A', 'Z' - *p + 1);

Now i got the message 开发者_如何学运维- assignment makes integer from pointer without a cast. Please help me out.


Change the for. Drop the * so you will correctly assign to a char *:

for(*p = s; *p; ++p, mov += m[L++])
    ^


s is an array of type char, you assign it to the dereferenced p which is a pointer of 'unsigned char'. You cannot do this without casting s:

The code that you probably want is:

for(p = (unsigned char *)s; *p; ++p, mov += m[L++])

by incrementing p in the for loop you go through all the values in the array s


*p i an unsigned char (deferencing a pointer to an unsigned char).

s is a pointer to a char (since it's an array).

So you're assigning a pointer to an unsigned char.

I would assume the compiler is saying that because you're making a char from a pointer without a cast.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜