Part Number: CODECOMPOSER
Greetings,
I'm trying to assign a structure content to another structure, but it gives me a behaviour which I don't understand.
Extract from an equivalent code:
_____________________________________"file1.c"_____________________________________
typedef struct smaller {
uint8_t a;
uint32_t b;
char c[8];
} smallerStructure;
typedef struct bigger {
smallerStructure *d;
smallerStructure *e;
smallerStructure *f;
} biggerStructure;
biggerStructure * myBiggerStructure1;
biggerStructure * myBiggerStructure2;
biggerStructure * myBiggerStructure3;
________________________________________________________________________________
_____________________________________"file2.c"_____________________________________
#include "file1.c"
void instantiateStructures (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 smaller structures inside the big structures*/
myBiggerStructure1 = g;
myBiggerStructure2 = h;
myBiggerStructure3 = i;
/*Instantiation works, meaning that now the global variables in file1.c myBiggerStructureX share same address as g, h or i, and also same addresses of smaller structures inside them, but they give me a global handle to access them.*/
}
void function (smallerStructure *test) {
*myBiggerStructure1->d = *test; //error. Doesn't assign and biggerStructure and d, e and f inside of it change their address to 0x00000000, 0x00000005, 0x00000400 or other values which are not always the same.
*(myBiggerStructure1->d) = *(test); //same error
memcpy(myBiggerStructure1->d,test,sizeof(*myBiggerStructure1); //same error
myBiggerStructure1->d->a = test->a;
myBiggerStructure1->d->b = test->b; //etc... single field assignment: same error.
memcpy(&(myBiggerStructure1->d->a),&(test->a),sizeof(*myBiggerStructure1); //same error.
myBiggerStructure1->d = test; //works, but it changes the address of d to the "test" one which I don't want.
}
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));
function(myTest);
}
________________________________________________________________________________
While calling instead:
function (myBiggerStructure1->d);
and changing the assignments appropriately inside that function makes me work on the considered structure directly, without problems. I don't understand why I can't work with assignments from a different structure.
Could you explain how your compiler manages these assignments?