结构数据和malloc;没有接收到等待的数据

结构数据和malloc;没有接收到等待的数据,c,memory,struct,memory-management,malloc,C,Memory,Struct,Memory Management,Malloc,我一直在研究如何在使用malloc声明的结构中更正实现日期 我有两个结构: typedef struct { int idWork; float area; //char tarifa[MAX_TARIFA_LENG]; int tarifa; } tWorkspace; typedef struct{ tPartner *socis; tWorkspace workP[50]; tContract *contract; int numSocis; int numWork; int numCo

我一直在研究如何在使用malloc声明的结构中更正实现日期

我有两个结构:

typedef struct {
int idWork;
float area;
//char tarifa[MAX_TARIFA_LENG];
int tarifa;
} tWorkspace;

typedef struct{
tPartner *socis;
tWorkspace workP[50];
tContract *contract;
int numSocis;
int numWork;
int numContract;
} tCoworking;
然后,我有一个我没有正确编码的函数

tCoworking* new_partner(tCoworking *a, int partnerId, char *nam, int descom){
bool exist = false;

a->socis=(tPartner*)malloc(sizeof(tPartner*));

printf("ID %d\n", partnerId);

//CHECK IF PARTNERID EXISTS IN THE LIST
if(a->numSocis != 0){
    for(int i=0; i<a->numSocis; i++){
        if(a->socis[i].id == partnerId){
            exist = true;
            printf("\nERROR: Partner %d is already in the list of partners\n",partnerId);
        }
    }
}
if(exist != true){
    //Check if there's no socis
    if(a->numSocis != 0){
        a->socis[a->numSocis].id = partnerId;
        strcpy(a->socis[a->numSocis].name, nam);
        a->socis[a->numSocis].descompte = descom;
        a->numSocis = a->numSocis+1;
    }else{
        a->socis[0].id = partnerId;
        strcpy(a->socis[0].name, nam);
        a->socis[0].descompte = descom;
        a->numSocis = 1;
    }
}
return a;
}
这就是我用数据调用函数的方式:

new_partner (&c, 11, "Anwar Sherman", 10);
当我第一次调用它时,
a->socis[0].id
给了我id 11,这是正确的

但当我再次称之为新合伙人时(&c,16,“Isa华纳”,20)索引0丢失以前的数据,并在索引1中正确写入数据

我的意思是,在第二次调用函数时,结构给了我以下信息:

索引0=110

指数1=16

如果我继续调用该函数,则先前的数据设置为0,并且我传递给它的数据“保存”

为什么我要使用malloc?我不知道我将获得的用户的确切数量,所以我尝试使用动态内存分配和动态结构

对不起,英语不好,解释不好,我已经尽力了

谢谢

编辑

更改为
a->socis=(tPartner*)malloc(sizeof(tPartner))

当我第三次使用不同的数据调用函数时,它看起来如下所示:

index 0 = 1852989783
index 1 = 0
index 2 = 24
包含数据的函数调用包括:

  new_partner (&c, 11, "Anwar Sherman", 10);
  new_partner (&c, 16, "Isa Warner", 20);
  new_partner (&c, 24, "Reagan Sawyer", 8);
更多示例:

new_partner (&c, 11, "Anwar Sherman", 10);
new_partner (&c, 16, "Isa Warner", 20);
new_partner (&c, 24, "Reagan Sawyer", 8);
new_partner (&c, 67, "Hashir Lloyd", 10);
我得到的是:

index 0 = 0
index 1 = 1394634337
index 3 = 0
index 4 = 67
尝试更改此选项:

a->socis = malloc(sizeof(tPartner*));
为此:

a->socis = malloc(sizeof(tPartner));
因为您要添加类型为
tPartner
的对象。这需要为该大小的对象分配内存,而不是像在代码中那样为其指针分配内存



附言:与您的问题无关:否。

malloc(sizeof(tPartner*))应该是
malloc(sizeof(tPartner))
。您需要足够的空间来容纳整个
tPartner
对象,但您只分配了足够的空间来存储指针。一般经验法则:在
malloc
内部
sizeof
的类型参数应该比分配结果的指针类型少一个
*
。或者,取消引用分配的指针,而不是尝试重写类型:
ptr=malloc(sizeof(*ptr))
。进行了更改。现在,当我第二次调用我的函数时,索引0是0,在它是110之前。现在它没有保存任何东西。我在回答我的问题哦,你每次调用函数都会覆盖
a->socis
,指针指向一个数组,该数组只包含足够一个
tPartner
的空间。似乎您希望它指向一个包含所有以前的
tPartners
的数组,因此您需要使用
realloc
来扩展它,而不是覆盖它。@NateEldredge这是有效的,如果您想添加这是一个答案,我将接受它。进行了更改。现在,当我第二次调用函数时,索引0是
0
,而不是
110
。现在它没有保存任何东西。我在回答我的问题
a->socis = malloc(sizeof(tPartner));