I have problem with C/C++ syntax in the following code.
I need to create an array of structs. Each struct contains a float variable and a pointer to another array of the same type. Pls note that I'm allocating memory dynamically because the real array size will be known at runtime (although constants are used in this example).
Everything works fine but I'm still having problem with the part commented by question marks. The allocation goes smooth but afterwards I have problems assigning the values to the newly created array.
Will appreciate any help.
Thanks a lot.
// sigle element structure
myElement *funcArgs; // pointer to array of structs
// pointer to the main array of elements
int main (int argc, char** argv)
// allocate array
myArray = (myElement*) malloc(sizeof (myElement) * 10);
if (myArray == NULL)
std::cout << "Memory allocation error" << std::endl;
// fill the elements
for (unsigned int i = 0; i < 10; i++)
(myArray + i)->fValue = i;
(myArray + i)->funcArgs = (myElement*) malloc(sizeof (myElement) * 2);
(myArray + i)->funcArgs = (myArray + i);
if (i == 0)
(myArray + i)->funcArgs = myArray + i;
(myArray + i)->funcArgs = myArray + i - 1;
// release memory
// i know, i'll have to release (myArray + i)->funcArgs pointers too