错误:分配到并返回到

时间:2016-10-20 05:31:23

标签: c linux pointers structure

我做了一个程序来更新一些类型结构但是在编译两个错误时,我的程序如下:

#include <stdio.h>

struct a {
    char *name;
    int id;
    char *department;
    int num;
};

typedef struct a ab;

void swap(ab *,ab *);
ab * update(ab *);

int main(int argc,char *argv[])
{
    ab array[2]={{"Saud",137,"Electronics",500},{"Ebad",111,"Telecom",570}};
    ab *q;

    *q=update(&array[0]);
}

ab * update(ab *p)
{
    p->name="Talha";
    p->id=234;
    p->department="Civil";
    p->num=567;

    return *p;
}

在编译时,分配和返回指针时出现两个错误。请通知我的错误,并对其进行更正。错误是,

newproject.c: In function ‘main’:

newproject.c:29:4: error: incompatible types when assigning to type ‘ab   {aka 
 struct a}’ from type ‘ab * {aka struct a *}’

*q=update(&array[0]);

  ^

newproject.c: In function ‘update’:

newproject.c:56:9: error: incompatible types when returning type ‘ab {aka 
struct a}’ but ‘ab * {aka struct a *}’ was expected

return *p;

       ^

1 个答案:

答案 0 :(得分:0)