函数返回结构时出错
这就是我想要做的:
我一直在处理创建结构的代码(在 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
info
不是类型,emp
只是一个struct info
类型的变量。添加 typedef(如果您想将emp
作为类型):...或者添加
struct
关键字。info
is not a type, andemp
is just a variable of typestruct info
. Either add a typedef (if you want toemp
as a type):...or add a
struct
keyword.emp
是变量,info
是类型。因此,您也应该在函数和主体的原型中使用info
而不是emp
。正如其他人所说,缺少
typedef
或缺少struct info
而不是emp
。我错过了那个:)恕我直言,这是您的代码中的两个不同的错误。
emp
is a variable,info
is the type. So you should useinfo
instead ofemp
in the prototype of your function and the body too.Like others said, a
typedef
is missing or astruct info
instead ofemp
. I missed that one :)IMHO those are two distinct errors in your code.
类型的名称是
struct info
,而不是info
。但更好的写法是:
emp *new_employees = malloc((record+1) * sizeof *emp);
The name of the type is
struct info
, notinfo
.But a better way to write that is:
emp *new_employees = malloc((record+1) * sizeof *emp);
您必须像这样定义函数:
或者使用 typedef 将结构定义为数据类型。
You must define the functions like this:
Or define the structures as a data type using typedef.