I'm trying to assign an array to one of the fields of a typedef struct and I can't find a way to do it practically.
I've searched for this problem but all I seem to find is answers for char * arrays which is not what I'm looking for, I'm just trying to assign an array to an int array, and looking for a practical way for the code below to work without having to initialize all the variables in the struct (they will be initialized later, but I just want to set the array variable):
typedef struct {
int array[5];
int number;
} Rot;
Rot RA;
void config()
{
RA.array = {1, 2, 3, 4, 5}; //This returns an "expected expression before "{"" error
int arr[5];
int i;
for (i = 0; i < 5; i++)
{
arr[i] = i + 1;
}
RA.array = arr; //I understand why this fails, but I need to do this in a practical way
}
Please assume that config is called later and the struct and RA are all accessible to it.
for (int i = 0; i < 5; ++i) RA.array[i] = arr[i];) or copying memory (e.g.memcpy(RA.array, arr, 5*sizeof(int))wherememcpy()is declared in standard header<string.h>).array = new int[] {1,2,3,4,5}you make it point to a different block of memory, which may have diffetent lifespan from your structure, thus you have to memcpy or re-assign each element in a for loop.