【发布时间】:2012-07-05 01:58:17
【问题描述】:
我有一个函数,我想返回一个 int 数组,但我不想为此创建一个变量,就像这样
int* foo()
{
static int bar[]={1,2,3}; // edit thanks to comments
return bar;
}
为了解决这个问题,我尝试了这个:
return (Uint8Type*) gAppCanContext.BluetoothMacAddr[0] + MacAddr[1] + '-'\
+ gAppCanContext.BluetoothMacAddr[2] + MacAddr[3] + '-' ;
但它不起作用。我也试过这个:
return (Uint8Type*[]){ MacAddr[0] , MacAddr[1] + '-' MacAddr[3] ... };
它编译时会发出警告,如果我执行程序,它会冻结。 我也尝试过使用星号和 & 符号,但无法正常工作。
有可能吗?如果有,怎么做?
补充:我不能使用 malloc - 是一个没有动态分配的嵌入式系统。
【问题讨论】:
-
您的
foo函数实际上不起作用。除了将字符串存储在int[]中之外,使用它的返回值会导致未定义的行为。使用malloc。 -
你不能在 C 中以这种方式连接字符串。字符串只是指针——添加会更改指针偏移量而不是附加字符。
-
那么,不管你的代码的问题,为什么讨厌变量?