函数返回结构时出错

发布于 2024-12-10 18:56:27 字数 1238 浏览 1 评论 0原文

这就是我想要做的:

我一直在处理创建结构的代码(在 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;

This is what I am trying to do:

I have been working on code that I have create a structure (hard coded in main) Then I want to malloc space for two structs (tryin to use functions). Then copy all the data in the first struct to the second struct and print the new structure.

The errors the occur is:
I don't understand what this error means.

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;

line 7

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

return new_employees;   
}

line 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;



}

header file:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十年九夏 2024-12-17 18:56:27

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);

info is not a type, and emp is just a variable of type struct info. Either add a typedef (if you want to emp as a type):

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

...or add a struct keyword.

struct info *create(int record);
struct info *copy(emp *data, int record);
难如初 2024-12-17 18:56:27

emp 是变量,info 是类型。因此,您也应该在函数和主体的原型中使用 info 而不是 emp

正如其他人所说,缺少 typedef 或缺少 struct info 而不是 emp。我错过了那个:)

恕我直言,这是您的代码中的两个不同的错误。

emp is a variable, info is the type. So you should use info instead of emp in the prototype of your function and the body too.

Like others said, a typedef is missing or a struct info instead of emp. I missed that one :)

IMHO those are two distinct errors in your code.

热鲨 2024-12-17 18:56:27
emp *new_employees = malloc(sizeof(info) * (record+1));

类型的名称是struct info,而不是info

但更好的写法是:

emp *new_employees = malloc((record+1) * sizeof *emp);

emp *new_employees = malloc(sizeof(info) * (record+1));

The name of the type is struct info, not info.

But a better way to write that is:

emp *new_employees = malloc((record+1) * sizeof *emp);

高冷爸爸 2024-12-17 18:56:27

您必须像这样定义函数:

struct emp *create()
struct emp *copy()

或者使用 typedef 将结构定义为数据类型。

You must define the functions like this:

struct emp *create()
struct emp *copy()

Or define the structures as a data type using typedef.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文