您好!
我正在尝试将结构内容分配给另一个结构、但它给了我一种我不理解的行为。
从等效代码提取:
_________________________________________ "file1.c"_________________________
typedef 结构更小{
uint8_t a;
uint32_t b;
字符 c[8];
}小结构体;
typedef 结构、大于{
小结构*d;
小结构*e;
小结构*f;
} biggerStructure;
biggerStructure * mybiggerStructure1;
biggerStructure * mybiggerStructure2;
biggerStructure * mybiggerStructure3;
________________________________________________________________
_________________________________________ "file2.c"_________________________
#include "file1.c"
void 实例结构(void){
biggerStructure *g = calloc (10、sizeof (smallerStructure));
biggerStructure *h = calloc (10、sizeof (smallerStructure));
biggerStructure *i = calloc (10、sizeof (smallerStructure));
/*I want to work with 10 sigant structures inside the big structures*/
myBiggerStructure1 = g;
myBiggerStructure2 = h;
myBiggerStructure3 = i;
/*Instantitation 起作用,这意味着 file1.c myBiggerStructureX 中的全局变量与 g、h 或 i 共享相同的地址,以及它们内部较小结构的相同地址,但它们为我提供了一个全局句柄来访问它们。*/
}
void 函数(smallerStructure *测试){
*myBiggerStructure1->d =*测试;//错误。 不分配和 biggerStructure and d、e 和 f、它们会将其地址更改为0x00000000、0x00000005、0x00000400或其他不总是相同的值。
*(myBiggerStructure1->d)=*(test);//相同错误
memcpy (myBiggerStructure1->d、test、sizeof (* myBiggerStructure1);//相同错误
myBiggerStructure1->d->a =测试->a;
myBiggerStructure1->d->b = test->b;//等等... 单个字段分配:相同错误。
memcpy (&(myBiggerStructure1->d->a)、&(test->a)、sizeof (* myBiggerStructure1);//相同错误。
myBiggerStructure1->d = test;//works、但它会将 d 的地址更改为我不想使用的"test"地址。
}
void higherFunction (void){
smallerStructure * myTest = calloc (1、sizeof (smallerStructure));
char testArray[8]={'q'、'q'、'q'、'q'、'q'、'q'、'q'、'q'};
myTest->A = 0;
myTest->b = 1;
memcpy (myTest->c、testArray、sizeof (testArray));
函数(myTest);
}
________________________________________________________________
调用时:
函数(myBiggerStructure1->d);
在该函数内适当地更改分配、可以直接处理所考虑的结构、而不会出现任何问题。 我不明白为什么我无法处理不同结构的任务。
您能否解释一下编译器如何管理这些分配?