C语言 表达式必须具有整数类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25761789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:21:26  来源:igfitidea点击:

expression must have integral type

cpointers

提问by Ogee Gee

I get that compilation error because of this line which intended to increase the pointer by 0x200 (to point to the next segment)

由于这一行打算将指针增加 0x200(指向下一段),因此我收到了编译错误

Flash_ptr = Flash_ptr + (unsigned char *) 0x200;

I'v seen thisbut I didn't use any illegal symbol!

我见过这个,但我没有使用任何非法符号!

P.S. The initialization of the pointer:

PS 指针的初始化:

unsigned char * Flash_ptr = (unsigned char *) 0x20000; 

回答by Paul R

You can't add two pointers. You can add an integer to a pointer, and you can subtract two pointers to get an integer difference, but adding two pointers makes no sense. To solve your problem therefore you just need to remove the cast:

你不能添加两个指针。可以将一个整数加到一个指针上,也可以减去两个指针得到一个整数差,但是两个指针相加是没有意义的。因此,要解决您的问题,您只需要删除演员表:

Flash_ptr = Flash_ptr + 0x200;

This increments Flash_ptrby 0x200 elements, but since Flash_ptris of type unsigned char *then this just translates to an increment of 0x200 bytes.

这会增加Flash_ptr0x200 个元素,但由于Flash_ptr是类型,unsigned char *所以这只是转换为 0x200字节的增量。

In order to make this part of a loop and check for an upper bound you would do something like this:

为了使这部分成为循环并检查上限,您将执行以下操作:

while (Flash_ptr < (unsigned char *)0x50000) // loop until Flash_ptr >= 0x50000
{
    // ... do something with Flash_ptr ...
    Flash_ptr += 0x200;
}

回答by Sourav Ghosh

You cannot addtwo pointers. What you can do is to incrementthe address held by the pointer. Remove the (unsigned char *)cast.

您不能添加两个指针。您可以做的是增加指针持有的地址。移除(unsigned char *)演员表。

If you're interested, read more about pointer arithmatic here.

如果您有兴趣,请在此处阅读有关指针算术的更多信息。