gpt4 book ai didi

c - 将嵌套结构指针分配给嵌套结构

转载 作者:行者123 更新时间:2023-11-30 16:07:08 24 4
gpt4 key购买 nike

我有一个实体结构和另一个带有指针的结构。程序的目的是:将实体结构分配给带有指针的结构,并使用其他结构指针访问每个实体结构成员。

我有问题陈述:由于两个结构成员不对称,当我将实体结构地址分配给带有指针的结构时,成员指针初始化会出错并导致系统崩溃。

有没有人有办法以优化的方式找到这个问题的解决方案?

------------------------------------------------------------ - - - - - - - - - - - - 程序 - - - - - - - - - - - - - ----

#include <stdio.h>
#include <string.h>
#include <stddef.h>


/* ===================== Binding Structure ================================= */
typedef struct
{
char id;
}tmodel;


typedef struct
{
char id;
}tbrand;

typedef struct
{
char id;
}tcommercialRef;

typedef struct
{
char id;
}tserialnum;


typedef struct
{
tmodel *smodel;
tbrand *sbrand;
tcommercialRef *scommref;
tserialnum *sslnum;
}tmetadata;

typedef struct
{
tmetadata *smetadata;
}tlink;

typedef struct
{
tlink *slink;
}trefernce;

typedef struct
{
char id[10];
int ttl;
int tss;
trefernce *sref;
}telectrical;



/* ===================== Application Strucuture ==============================*/
void filldata(telectrical *elec);


typedef struct
{
tmodel smodel;
tbrand sbrand;
tcommercialRef scommref;
tserialnum sslnum;
}Ymetadata;

typedef struct
{
Ymetadata smetadata;
}slink;

typedef struct
{
slink glink;
}refernce;

typedef struct
{
char id[10];
int ttl;
int tss;
refernce grefernce;
}gtelectrical;


//solid strucutre object
gtelectrical obj;

//structure pointer object
telectrical *elec = {0};

/* =============================== main.c =================================== */
int main()
{
printf("test");

//static void **p = (void *)&elec;

obj.tss = 55;
obj.ttl = 100;



obj.grefernce.glink.smetadata.smodel.id = 5;
obj.grefernce.glink.smetadata.sbrand.id = 6;
obj.grefernce.glink.smetadata.scommref.id = 7;
obj.grefernce.glink.smetadata.sslnum.id = 8;


elec = (telectrical *)&obj;

//elec structure -> sref pointer goes bad as it's not same type as "grefernce"

//*p = (void *)&obj;


//static long x = (long) offsetof( telectrical, sref);

//(long) offsetof(struct telectrical, sref);

//*(*p + x) = obj.grefernce.glink.smetadata.;


elec->id[0] = 0;
elec->id[1] = 1;
elec->id[2] = 2;

elec->ttl = 5;
elec->tss = 10;

elec->sref->slink->smetadata->sslnum->id = 4;
elec->sref->slink->smetadata->sbrand->id = 1;
elec->sref->slink->smetadata->scommref->id = 2;
elec->sref->slink->smetadata->smodel->id = 3;

//filldata(elec);

printf("------");

printf("%d\n",elec->sref->slink->smetadata->sslnum->id);
printf("%d\n",elec->sref->slink->smetadata->sbrand->id);
printf("%d\n",elec->sref->slink->smetadata->scommref->id);
printf("%d\n",elec->sref->slink->smetadata->smodel->id);


return 0;
}


/* //////////////////////////////////////// user scope ////////////////////////////// */
void filldata(telectrical *pelec)
{
pelec->id[0] = 0;
pelec->id[1] = 1;
pelec->id[2] = 2;

pelec->ttl = 5;
pelec->tss = 10;

//pelec->sref->slink->smetadata->sslnum->id = 4;
//pelec->sref->slink->smetadata->sbrand->id = 1;
//pelec->sref->slink->smetadata->scommref->id = 2;
//pelec->sref->slink->smetadata->smodel->id = 3;

}

最佳答案

您没有为指向另一个结构中存在的其他结构的指针分配内存。以下内容可能会帮助您进行多级内存分配和分配:

#include<stdio.h>
#include<stdlib.h>

typedef struct A
{
int i;
}A_Node;

typedef struct B
{
A_Node *A_ptr;
}B_Node;

typedef struct C
{
B_Node *B_ptr;
}C_Node;

int main(void)
{
//ACCESSING-MANIPULATING A USING B
B_Node B_obj;

B_obj.A_ptr=malloc(sizeof(*(B_obj.A_ptr)));

(B_obj.A_ptr)->i=192;

A_Node A_obj=*(B_obj.A_ptr); //For checking if the allocation is successful and good

printf("%d\n",A_obj.i);


//ACCESSING-MANIPULATING A USING C

C_Node C_obj;
C_obj.B_ptr=malloc(sizeof(*(C_obj.B_ptr))); //allocating space for struct of B using C object

(C_obj.B_ptr)->A_ptr = malloc(sizeof(*((C_obj.B_ptr)->A_ptr))); //allocating space for struct of A using B Struct for which space was allocated in previous step by C struct

((C_obj.B_ptr)->A_ptr)->i=876;

A_obj=*((C_obj.B_ptr)->A_ptr); //For checking if the allocation is successful and good
printf("%d\n",A_obj.i);

return 0;
}

阅读代码并询问是否有任何疑问,以类似的方式可以创建这个多级struct-inside-struct(尽管它会很难看)。

关于c - 将嵌套结构指针分配给嵌套结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59764273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com