在c中返回结构
我正在编写一个程序来定义一个结构,然后编写一个函数来创建并返回以前定义的结构。我的结构如下:在c中返回结构,c,arrays,pointers,struct,return,C,Arrays,Pointers,Struct,Return,我正在编写一个程序来定义一个结构,然后编写一个函数来创建并返回以前定义的结构。我的结构如下: struct Employee{ char name[MAX_NAMES];//Symbolic constant with max set to 200 int birthYear; int startYear; }; 我的职能是: struct Employee* makeEmployee(char* nameOf, int birthYearOf, int st
struct Employee{
char name[MAX_NAMES];//Symbolic constant with max set to 200
int birthYear;
int startYear;
};
我的职能是:
struct Employee* makeEmployee(char* nameOf, int birthYearOf, int startYearOf)
{
struct Employee *e;
e = (struct Employee *) malloc(sizeof(struct Employee));
(*e).name = mystrcpy((*e).name, *nameOf);
(*e).birthYear = birthYearOf;
(*e).startYear = startYearOf;
return e;
}
//edited in from comments below OP:
char* mystrcpy(char dest, const char src)
{
while((*dest++ = *src++) != '\0')
{
;
}
return dest;
}
我得到的错误是:mystring.c:在函数“makeEmployee”中:
mystring.c:147:警告:传递'strcpy'的参数2将从整数生成指针,而不进行强制转换
mystring.c:147:错误:赋值中的类型不兼容
在此处输入代码几件事:mystrcpy的原型需要为字符串使用参数,而不是为char使用值:
更改:
char* mystrcpy(char dest, const char src){...}
e = (struct Employee *) malloc(sizeof(struct Employee));
e = malloc(sizeof(struct Employee));
至:
<>在C中,MaLoc的返回不正确(它是C++的,不是C的)。
更改:
char* mystrcpy(char dest, const char src){...}
e = (struct Employee *) malloc(sizeof(struct Employee));
e = malloc(sizeof(struct Employee));
至:
char* mystrcpy(char dest, const char src){...}
e = (struct Employee *) malloc(sizeof(struct Employee));
e = malloc(sizeof(struct Employee));
在此段中,您试图使用=
运算符为字符串赋值:
(*e).name = mystrcpy((*e).name, *nameOf);
^//not correct
使用字符串函数指定字符串。例如:
strcpy((*e).name, mystrcpy((*e).name, nameOf)); (or your custom version, mystrcpy once it works)
//(although this is redundant)
mystrcpy((*e).name, mystrcpy((*e).name, nameOf));
您的自定义mystrcpy()
需要确保返回以null结尾的字符串。此版本修改为使用[]
运算符和显式索引(i)以确保插图的可读性:
char* mystrcpy(char *dest, const char *src)
{
int i=0;
while((src[i]) != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = 0;//null terminate copied string
return dest;
}
下面是代码的可编译版本,包括上面提到的修改:
#define MAX_NAMES 80
struct Employee{
char name[MAX_NAMES];//Symbolic constant with max set to 200
int birthYear;
int startYear;
};
//Prototypes:
struct Employee* makeEmployee(char* nameOf, int birthYearOf, int startYearOf);
char* mystrcpy(char *dest, const char *src);
int main(void)
{
struct Employee Emp, *pEmp; //create and initialize an instance and pointer to struct
pEmp = &Emp;//note, there is nothing to malloc when initializing this way.
pEmp = makeEmployee("somename", 2007, 2015);
//do something with pEmp here
free(pEmp);//memory allocated in makeEmployee must be freed
//by the way, if you prototyped your function
//to include the pointer to struct as an argument
//it can be malloc'd and free'd in the same function
return 0;
}
struct Employee* makeEmployee(char* nameOf, int birthYearOf, int startYearOf)
{
struct Employee *e;
e = malloc(sizeof(struct Employee));
strcpy((*e).name, mystrcpy((*e).name, nameOf));
// or use mystrcpy((*e).name, mystrcpy((*e).name, nameOf));
(*e).birthYear = birthYearOf;
(*e).startYear = startYearOf;
return e;
}
char* mystrcpy(char *dest, const char *src)
{
int i=0;
while((src[i]) != '\0')
{
dest[i] = src[i];
i++;
}
dest[i] = 0;
return dest;
}
要返回结构,只需通过指针返回(地址副本)。所以你会写:
void creat(struct employee *emp, char *name, int birthYear, int startYear)
{
strcpy(emp->name, name);
emp->birthYear = birthYear;
emp->startYear = startYear;
}
来电者:
struct employee bob;
creat(&bob, "Bob", 1985, 2000);
现在,您可以像普通员工一样使用
bob
。您需要显示实际生成错误的行,否则没有人能够看到那里发生了什么。我在任何地方都看不到“strcpy”,它是宏吗?如果是这样,你需要显示它。写e->name
,而不是(*e).name
。错误(可能)是mystrcpy()
的第二个参数,它应该是char
指针(nameOf
),而不是char
(*nameOf
)。mystrcpy
看起来像什么?对不起,我应该放在那里。mystrcpy是char*mystrcpy(char dest,const char src){while(*dest++=*src++)!='\0'{;}return dest;}很抱歉,我是这个网站的新手,代码没有格式化。也可以按值返回结构。