C StructuresLesson 38Free preview
C Structs & Pointers
This lesson is part of C · All levels
Summary
Often struct is sent via pointer so that it is not necessary to copy the entire contents into the function's memory. To access the member through a pointer, an arrow operator is used, e.g. p->nilai, which is equivalent to (*p).nilai but more concise and not prone to parenthesis misplacement. This pattern forms the basis for implementing data structures such as linked lists and trees in C.
Key points
- ptr->member is an abbreviation of (*ptr).member to access a member via a pointer.
- Sending a pointer to a function saves memory and allows the function to change the original data.
- Use &obj to get the address of a struct when you refer it to a function.
- Make sure the pointer points to a valid struct before de-referencing, otherwise the program could crash.
- A pointer to a struct can be a member of a struct, for example the next pointer in a linked-list node.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.