Concept
How do you dynamically allocate and initialize a structure?
ComputerScienceOne / Selection Operators
"Student *s = (Student *) malloc(sizeof(Student) * 1); The multiplication by 1 in this example is not strictly necessary, but emphasizes the fact that we are allocating space for one structure and not an array of structures. Initializing a dynamically allocated structure like this does not initialize any of its variables as there are no default values defined by C. Moreover, it does not initialize memory for any pointer member variables. For example the firstName and lastName pointers need to be manually initialized with additional malloc() calls."
Related Ideas
- How do you access members through a pointer to a structure?ComputerScienceOne · Selection Operators
- How can structures be dynamically allocated?ComputerScienceOne · Declaration & Initialization
- What topics are listed in this contents excerpt?ComputerScienceOne · Selection Operators . . . . . . . . . . . . . . . . . . . . . . . . . .
- How are structure instances declared and initialized?ComputerScienceOne · Declaration & Initialization
- How can one structure be composed of another structure?ComputerScienceOne · Declaration & Initialization