简体   繁体   English

从'long int'到'void(*)()'的无效转换[-fpermissive]

[英]invalid conversion from 'long int' to 'void (*)()' [-fpermissive]

Hi I am writing little modified code for the bootloader for MCU's. 嗨,我正在为MCU的引导程序编写一些修改过的代码。 The modification I am trying to do is power the boot loader even for an watchdog timer reset. 我试图做的修改是为引导加载程序供电,即使是看门狗定时器复位也是如此。

I am using this function prototype to define the address of the boot loader and I get the error : 我正在使用此函数原型定义引导加载程序的地址,但出现错误:

invalid conversion from 'long int' to 'void (*)()' [-fpermissive] 从'long int'到'void(*)()'的无效转换[-fpermissive]

My code is 我的代码是

#if defined ( __AVR_ATmega1284P__ )
    void (*boot_start)(void) = 0xF000;
#elif defined ( __AVR_ATmega2560__ ) 
    void (*boot_start)(void) = 0x1F000;
#endif

Where the 0xF000 and 0x1F000 are memory spaces. 其中0xF000和0x1F000是内存空间。 I don't get this error if my code is ` 如果我的代码是`,我不会收到此错误

void (*boot_start)(void) = 0x0000;

Why ?? 为什么??

0x0000只是NULL的另一个名称,可以将ok编译为指针值,但是其他值需要显式转换为正确的类型。

Compiler recognize 0xF000 as int and discard assigning this value to pointer. 编译器将0xF000识别为int并放弃将此值分配给指针。 You should cast it explicitly: 您应该显式转换它:

void (*boot_start)(void) = (void (*)())0xF000;

The answer that I got from AVRFreak forum, 我从AVRFreak论坛获得的答案,

USE TYPEDEF, 使用TYPEDEF

typedef void (*fptr_t)(void);

fptr_t boot_start = (fptr_t)0xF000;

...
  boot_start();

The reason you get the warning otherwise is that 0xF000 is a long int for an AVR. 否则得到警告的原因是0xF000对于AVR是long int。 The thing on the left of '=' was a pointer to a function. '='左边的东西是一个指向函数的指针。 So you are trying to assign an integer to a pointer. 因此,您正在尝试为指针分配一个整数。 C thinks this is probably a mistake (often it is!) so it warns you. C认为这可能是一个错误(通常是一个错误!),因此警告您。 The way you quell that warning is to say "no this 0xF000 number really is a pointer value". 您平息该警告的方式是说:“此0xF000数字确实不是指针值”。 The way you do that is with a typecast. 您的方式是使用类型转换。 Now you could do this with (wait for it): 现在,您可以使用(等待)进行此操作:

void (*boot_start)(void) = (void(*)(void))0xF000;

But as you can see in that there is almost exactly the same (quite complex) structure on both side of the equals. 但是正如您所看到的,在等式的两边几乎都有完全相同的(相当复杂的)结构。 So it makes sense to put all that detail into one single typedef and use it in multiple places. 因此,将所有细节放入一个单独的typedef中并在多个地方使用它是有意义的。

If the function type were more complex this might even be something like: 如果函数类型更复杂,则可能甚至是这样:

int (*boot_start)(char, long, int *) = (int (*)(char, long, int *))0xF000;

and this starts to look very silly indeed - not only is it very likely you make a typing error it's just trying to remember the syntax here is a real pain! 这确实看起来非常愚蠢-不仅很可能您输入错误,而且只是想记住这里的语法,真是太痛苦了! So you use typedef to define the function interface just once: 因此,只需使用typedef一次定义函数接口:

typedef int (*myfn_t)(char, long, int *);

myfn_t boot_start  = (myfn_t))0xF000;

and that gets to be easier to type and easier to manage. 而且变得更容易输入和管理。 If you later add a fourth char ** parameter to the function you now do it in just one place - the typedef. 如果以后再向函数添加第四个char **参数,则现在只需要在一个地方-typedef。

Thanks to C Lawson and Yuriy. 感谢C Lawson和Yuriy。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:从'int'到'void *'的无效转换[-fpermissive] - error: invalid conversion from 'int' to 'void*' [-fpermissive] 错误:从'void *'到'test :: apr_size_t * {aka long unsigned int *}'的无效转换'[-fpermissive] - error: invalid conversion from ‘void*’ to ‘test::apr_size_t* {aka long unsigned int*}’ [-fpermissive] 从'long int'到'long int *'的无效转换-fpermissive - invalid conversion from 'long int' to 'long int*' -fpermissive 无效转换从void *到void(*)(void *)[ - fpermissive] - invalid conversion from void* to void(*)(void*)[-fpermissive] 错误:在给定的命令集中从 'int' 到 'void*' [-fpermissive] 的无效转换 - error: invalid conversion from ‘int’ to ‘void*’ [-fpermissive] in the given set of commands g ++编译器:从'pthread_t {aka long unsigned int}'到'void *(*)(void *)'的错误无效转换[-fpermissive] - g++ compiler: error invalid conversion from 'pthread_t {aka long unsigned int}' to 'void* (*)(void*)' [-fpermissive] 错误:从'int(*)[6]'到'int'的无效转换[-fpermissive] | - error: invalid conversion from 'int (*)[6]' to 'int' [-fpermissive]| 从'int'到int *的无效转换[-fpermissive] - invalid conversion from 'int' to int* [-fpermissive] 错误:从'int'到'int *'的无效转换[-fpermissive] - Error: invalid conversion from 'int' to 'int*' [-fpermissive] 从int *到int [-fpermissive]的无效转换 - invalid conversion from int* to int [-fpermissive]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM