|
|
前往: |
| 可否让一个函数的返回值可以是任意值呢?(USMART设计中遇到的问题) |
| 发表人 | 内容 | |
|---|---|---|
| 2011/04/28 14:23 |
|
|
|
[楼主位] 正点原子
等级:
注册时间:![]() 2010/12/02 10:41 文章: 8097 来自: 湖南 离线 |
在USMART(一个串口调试互交组建)的设计过程中,需要一个通用的函数原型,来匹配各种函数(不包含指针参量,即参数和返回值都不为指针类型).以达到函数受usmart管理的目的. //定义一个通用的函数类型
图片不清晰,看下面的: ..\USMART\usmart_config.c(22): warning: #144-D: a value of type "void (*)(u16)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(23): warning: #144-D: a value of type "void (*)(u32)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(24): warning: #144-D: a value of type "u16 (*)(void)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(25): warning: #144-D: a value of type "void (*)(u8, u32, u8)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(26): warning: #144-D: a value of type "void (*)(u8, u8)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(27): warning: #144-D: a value of type "u8 (*)(u16, u8, u8, u8, u8, u8)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(28): warning: #144-D: a value of type "void (*)(void)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(29): warning: #144-D: a value of type "void (*)(u8, u8 *)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(30): warning: #144-D: a value of type "void (*)(u8, u8)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(31): warning: #144-D: a value of type "u8 (*)(void)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(32): warning: #144-D: a value of type "void (*)(u8, u8)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(33): warning: #144-D: a value of type "void (*)(void)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(34): warning: #144-D: a value of type "u8 (*)(u8, u8)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(35): warning: #144-D: a value of type "void (*)(void)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(36): warning: #144-D: a value of type "void (*)(void)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(37): warning: #144-D: a value of type "void (*)(void)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(38): warning: #144-D: a value of type "void (*)(u32)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(39): warning: #144-D: a value of type "void (*)(u32)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(40): warning: #144-D: a value of type "void (*)(u8, u8, u8)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(42): warning: #144-D: a value of type "void (*)(void)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(43): warning: #144-D: a value of type "void (*)(void)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(44): warning: #144-D: a value of type "u8 (*)(u8, u8, u8)" cannot be used to initialize an entity of type "usmart_func" ..\USMART\usmart_config.c(45): warning: #144-D: a value of type "u8 (*)(u8, u8, u8)" cannot be used to initialize an entity of type "usmart_func"
这篇文章被编辑了 3 次. 最近一次更新是在 2011/04/28 14:56 |
|
|
我的淘宝小店:http://shop62103354.taobao.com |
||
|
||
| 2011/04/28 14:29 |
|
|
|
[1楼] 正点原子
等级:
注册时间:![]() 2010/12/02 10:41 文章: 8097 来自: 湖南 离线 |
在百度上搜到,有人说可以用联合体,但是我试过,警告依旧.(测试软件是MDK3.80A).
|
|
|
我的淘宝小店:http://shop62103354.taobao.com |
||
|
||
| 2011/05/23 22:14 |
|
|
|
[2楼] aozima
等级:
注册时间:![]() 2011/05/23 22:03 文章: 3 离线 |
[code]
#include <stdio.h> #include <stdlib.h> struct _m_usmart_nametab { void * func; //函数指针 char * name; //函数名(查找串) }; void test1(void) { printf("fun test1 done!\r\n"); return; } int test2(void) { printf("fun test2 done!\r\n"); return 2; } int test3(int arg) { printf("arg is :%d\r\n",arg); printf("fun test3 done!\r\n"); return arg*2; } unsigned long test4(int arg1,unsigned long arg2) { printf("arg1 is :%d\r\n",arg1); printf("arg2 is :0x%X\r\n",arg2); printf("fun test4 done!\r\n"); return arg2+1; } struct _m_usmart_nametab usmart_nametab[]= { {test1,"void test1(void)",}, {test2,"int test2(void)",}, {test3,"int test3(int arg)",}, {test4,"unsigned long test4(int arg1,unsigned long arg2)",}, }; int main() { unsigned long return_value = 0; unsigned int return_long = 0; // 函数类型 (*指针变量名)(形参列表); printf("function name: %s\r\n",usmart_nametab[0].name); test1(); printf("***\r\nfunction table:\r\n"); return_value = (*(unsigned long(*)())usmart_nametab[0].func)(); printf("return_value is : %d\r\n",return_value); printf("\r\nfunction name: %s\r\n",usmart_nametab[1].name); printf("return_value is : %d\r\n",test2()); printf("***\r\nfunction table:\r\n"); return_value = (*(int(*)())usmart_nametab[1].func)(); printf("return_value is : %d\r\n",return_value); printf("\r\nfunction name: %s\r\n",usmart_nametab[2].name); printf("return_value is : %d\r\n",test3(4)); printf("***\r\nfunction table:\r\n"); return_value = (*(int(*)(int))usmart_nametab[2].func)(5); printf("return_value is : %d\r\n",return_value); printf("\r\nfunction name: %s\r\n",usmart_nametab[3].name); printf("return_value is : %X\r\n",test4(4,0xFFFFFFF7)); printf("***\r\nfunction table:\r\n"); return_long = (*(unsigned long(*)(int,unsigned long))usmart_nametab[3].func)(5,0xFFFFFFF8); printf("return_long is : %X\r\n",return_long); return 0; } [/code] |
|
|
||
| 2011/05/23 23:07 |
|
|
|
[3楼] 正点原子
等级:
注册时间:![]() 2010/12/02 10:41 文章: 8097 来自: 湖南 离线 |
回复【2楼】 aozima :
--------------------------------- 谢谢.后来给我的代码可用. struct _m_usmart_nametab { void * func; //函数指针 char * name; //函数名(查找串) }; void test1(void) { printf("fun test1 done!\r\n"); return; } int test2(void) { printf("fun test2 done!\r\n"); return 2; } int test3(int arg) { printf("arg is :%d\r\n",arg); printf("fun test3 done!\r\n"); return arg*2; } unsigned long test4(int arg1,unsigned long arg2) { printf("arg1 is :%d\r\n",arg1); printf("arg2 is :0x%X\r\n",arg2); printf("fun test4 done!\r\n"); return arg2+1; } struct _m_usmart_nametab usmart_nametab[]= { {(void*)test1,"void test1(void)",}, {(void*)test2,"int test2(void)",}, {(void*)test3,"int test3(int arg)",}, {(void*)test4,"unsigned long test4(int arg1,unsigned long arg2)",}, }; int main(void) { //启用PLL运行在72M // SystemInit(); printf("hello"); // 测试 { unsigned long return_value = 0; unsigned int return_long = 0; // 函数类型 (*指针变量名)(形参列表); printf("function name: %s\r\n",usmart_nametab[0].name); test1(); printf("***\r\nfunction table:\r\n"); return_value = (*(unsigned long(*)())usmart_nametab[0].func)(); printf("return_value is : %d\r\n",return_value); printf("\r\nfunction name: %s\r\n",usmart_nametab[1].name); printf("return_value is : %d\r\n",test2()); printf("***\r\nfunction table:\r\n"); return_value = (*(int(*)())usmart_nametab[1].func)(); printf("return_value is : %d\r\n",return_value); printf("\r\nfunction name: %s\r\n",usmart_nametab[2].name); printf("return_value is : %d\r\n",test3(4)); printf("***\r\nfunction table:\r\n"); return_value = (*(int(*)(int))usmart_nametab[2].func)(5); printf("return_value is : %d\r\n",return_value); printf("\r\nfunction name: %s\r\n",usmart_nametab[3].name); printf("return_value is : %X\r\n",test4(4,0xFFFFFFF7)); printf("***\r\nfunction table:\r\n"); return_long = (*(unsigned long(*)(int,unsigned long))usmart_nametab[3].func)(5,0xFFFFFFF8); printf("return_long is : %X\r\n",return_long); } // 测试 } |
|
|
我的淘宝小店:http://shop62103354.taobao.com |
||
|
||
| 2011/05/23 23:10 |
|
|
|
[4楼] 正点原子
等级:
注册时间:![]() 2010/12/02 10:41 文章: 8097 来自: 湖南 离线 |
在aozima的帮助下,过几天我会公布USMARTV1.4的源码.
V1.4的主要新特性: 1,支持字符串参数. 2,优化参数存储方式,内存占用显著减小. 3,自适应参数长度(连函数名,总数不超过64个字节.ps:只需要自行修改usart.c的部分内容,即可支持大于64字节.) 4,去掉了警告(在aozima帮助下解决,特此感谢!). |
|
|
我的淘宝小店:http://shop62103354.taobao.com |
||
|
||
|
|
||
|
| 前往: |