Array of structures | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

25th May 2021, 1:41 PM
Max Smith
Max Smith - avatar
5 Answers
+ 3
Max, Why are you using a `union` in that data structure? I don't understand what's the need of a `union` there ...
25th May 2021, 2:19 PM
Ipang
+ 1
You should include string.h and use strcpy to assign value to char array
25th May 2021, 1:48 PM
Michal Doruch
+ 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;
25th May 2021, 2:14 PM
Lofty
Lofty - avatar
+ 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.
25th May 2021, 3:16 PM
Max Smith
Max Smith - avatar
+ 1
Btw, thanks to all replies, now I got it.
25th May 2021, 3:17 PM
Max Smith
Max Smith - avatar