c - 函数返回结构时出错

标签 c struct structure

这就是我正在尝试做的事情:

我一直在编写创建结构的代码(在 main 中硬编码),然后我想为两个结构分配空间(尝试使用函数)。然后将第一个结构中的所有数据复制到第二个结构中并打印新的结构。

出现的错误是: 我不明白这个错误是什么意思。

pointer.c:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
pointer.c:13: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token



#include <stdio.h>
#include <stdlib.h>
#include "pointer.h"
int rec = 0;

第 7 行

struct emp *create(int record){
emp *new_employees = malloc(sizeof(info) * (record+1));

return new_employees;   
}

第 13 行

struct emp *copy(emp *data, int record){
emp *new_employee = create(record+1);
int i;
for(i = 0; i<record;i++){
    new_employee.first = data.first;
    new_employee.last = data.last;
    new_employee.start_date = data.start_date;
    new_employess.sal = data.sal;
    data++;
}
return new_employee;
}


int main(void){
struct info employees;
employees.first = "FIRST";
employees.last = "LAST";
employees.start_date = "June-20th-2006";
employees.sal = 55555.55;
rec = rec+1;



}

头文件:

#include <string.h>
struct info {
char *first;
char *last;
char *start_date;
float sal;
} emp;

最佳答案

info 不是类型,emp 只是一个 struct info 类型的变量。添加 typedef(如果您想将 emp 作为类型):

typedef struct info {
    char *first;
    char *last;
    char *start_date;
    float sal;
} emp;

...或添加 struct 关键字。

struct info *create(int record);
struct info *copy(emp *data, int record);

关于c - 函数返回结构时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7840318/

相关文章:

c++ - 是否存在我遗漏的运算符优先级问题? unsigned short 与 inverse 的比较失败

将 C 代码转换为 Delphi

objective-c - 在 C/Objective-C 中使用带有系统函数的 cd ~ 不能正确地 cd

c++ - 将 void* 转换为 struct*

c - 定义未定义数组并从 c 中的结构返回时出现段错误

c - 理解结构函数 *adds 和 rel_list

c - 尽管文件非空,但读取到缓冲区的内容为空

c++ - 'int [0]' c++ 的初始值设定项太多

c - 包含苹果的网格

c - 为什么在每次写入之前打开文件并在每次写入之后立即关闭它不是一个好主意?