This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CODECOMPOSER: Structure content assignment to another structure

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?

  • There is more than one problem in your code.  For now, I will focus on one error.  

    This line ...

    biggerStructure *g = calloc(10,sizeof(smallerStructure));

    followed by ...

    myBiggerStructure1 = g;

    ... only initializes myBiggerStructure1->d to 0.  It does not initialize d to point to a useful address in memory such that an assignment like ...

    *myBiggerStructure1->d = *test;

    ... can work.  That attempts to write the contents of *test to the address 0.  I'm surprised some kind of fault did not occur.  

    Speaking generally ... Your understanding of pointers and memory allocation needs to improve.  This forum is not the best place for such discussion.  I recommend you start with a book, tutorial, etc. on the C programming language.  Or, maybe a more experienced co-worker.  

    Thanks and regards,

    -George