在c中返回结构

时间:2015-09-18 23:42:02

标签: c arrays pointers struct return

我正在编写一个程序来定义一个结构,然后编写一个函数来创建和返回一个先前定义的结构。我的结构如下:

 struct Employee{
    char name[MAX_NAMES];//Symbolic constant with max set to 200
    int birthYear;
    int startYear;
    };

我的功能[s]是[是]:

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:错误:赋值中不兼容的类型

在这里输入代码

2 个答案:

答案 0 :(得分:0)

有几件事情
mystrcpy的原型需要使用字符串的参数,而不是char的值:
更改:

char* mystrcpy(char dest, const char src){...}

<强> 到:

char* mystrcpy(char *dest, const char *src){...} 

在C中,转换malloc的返回是不正确的(它在C ++中,而不是C) 更改:

  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()需要确保它返回一个以空字符结尾的字符串。修改此版本以使用[]运算符和显式索引(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; 
}

答案 1 :(得分:0)

要返回结构,只需通过指针返回(地址副本)。所以你要写:

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