是否所有的C编译器允许函数返回结构? [英] Do all C compilers allow functions to return structures?
问题描述
我工作在C程序,并使用了8051架构设备SDCC编译器。
我想编写一个名为的GetName函数,将读取闪存8个字符,并以某种形式返回字符数组。我知道这是不可能在C返回一个数组,所以我想用做这样的结构:
I am working on a program in C and using the SDCC compiler for a 8051 architecture device. I am trying to write a function called GetName that will read 8 characters from Flash Memory and return the character array in some form. I know that it is not possible to return an array in C so I am trying to do it using a struct like this:
//********************FLASH.h file*******************************
MyStruct GetName(int i); //Function prototype
#define NAME_SIZE 8
typedef struct
{
char Name[NAME_SIZE];
} MyStruct;
extern MyStruct GetName(int i);
// *****************FLASH.c file***********************************
#include "FLASH.h"
MyStruct GetName( int i)
{
MyStruct newNameStruct;
//...
// Fill the array by reading data from Flash
//...
return newNameStruct;
}
我没有这个功能的任何引用但但由于某种原因,我得到一个说:编译器错误功能不能返回汇总。这是否意味着我的编译器不支持返回结构的功能呢?还是我做错了什么?
I don't have any references to this function yet but for some reason, I get a compiler error that says "Function cannot return aggregate." Does this mean that my compiler does not support functions that return structs? Or am I just doing something wrong?
推荐答案
SDCC不支持分配和返回结构尚未(如果他们的Wiki是先进的为准):
SDCC doesn't support assignment and returning structs yet (if their Wiki is up-to-date):
尚未在SDCC实现的:
Not yet implemented in sdcc:
- 数据类型double。
- 结构和工会不能给,如函数参数传递和返回值。
- 寄存器存储说明函数的参数。
也许你应该做一个
void GetName(MyStruct* ret_name, int i);
函数。
这就是说,你应该把函数原型的 的的主
和的前后的的 MYSTRUCT
。如果没有原型的函数将被假定为返回 INT
。
That said, you should put the function prototype before the main
and after the MyStruct
. If there's no prototypes a function will be assumed to return int
.
MyStruct GetName(int i);
void main(void) { ...
(另外,主
函数应该是 INT主要(无效)
或 INT主(INT ARGC,字符** argv的)
,它不应该返回无效
)
(Also, the main
function should be an int main(void)
or int main(int argc, char** argv)
. It shouldn't return void
.)
这篇关于是否所有的C编译器允许函数返回结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!