0
Array of structures
I have read the course about dynamical memory allocation and tried to make a sample code, but it's not working. I have read the error, but I don't quite get what's wrong about it. https://code.sololearn.com/c93W5QAl7nkT/?ref=app
5 odpowiedzi
+ 3
Max,
Why are you using a `union` in that data structure? I don't understand what's the need of a `union` there ...
+ 1
You should include string.h and use strcpy to assign value to char array
+ 1
Your error has nothing to do with dynamical memory allocation. The error is that you're trying to assign a value to an array.
char job_description[30];
(people + i)->employee.job.job_description = "Dev";
Check this simple snippet:
// You can initialize an array with string literal:
char array[30] = "Dev";
// But you can't assign it:
array = " Hi Dev";
// Compile-time error: assignment to expression with array type
One way to fix this is by using pointer:
Change
char job_description[30];
Into:
char* job_description;
+ 1
I just wanted to see how to work with an union inside a structure. I could also put this as a string inside the structure, but I just wanted to see how it works inside.
+ 1
Btw, thanks to all replies, now I got it.