In C, structs are similar to those in other languages, but there are some key differences:
Memory Management: In C, you’re responsible for memory management. When creating structs dynamically (like in newPerson), you need to manually allocate memory with malloc and free it when you’re done.
String Handling: C doesn’t have a built-in string type. Strings are represented as char arrays or pointers. When assigning strings, you need to be careful about memory allocation (like using strdup).
No Method Syntax: C doesn’t have methods associated with structs. Instead, you typically pass a pointer to the struct as the first argument to functions that operate on it.
Initialization: C99 and later allow designated initializers (like .name = "Fred"), which is similar to named field initialization in some other languages.
Pointers: In C, you often work with pointers to structs. The -> operator is used to access fields through a pointer, while . is used for direct struct access.
No Garbage Collection: C doesn’t have garbage collection, so you need to manually free any dynamically allocated memory to prevent memory leaks.
To compile and run this program:
This C code demonstrates the basics of working with structs, including creation, initialization, access, and memory management.