13_pdfsam_cs2022_lectures - Pointers to structures Function...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Pointers to structures Function calls with structure arguments can be costly Solution: Use pointers to structures. Only the address of the structure is copied, not the entire structure. Use *p to get the structure pointed to by p Use (*p).field to access a field Or the cleaner syntax: p->fieldU
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Pointers to structures Function calls with structure arguments can be costly Solution: Use pointers to structures. Only the address of the structure is copied, not the entire structure. typedef struct { char name[10000]; int id; } Person; void print_person(Person * p) { printf(“%d %s\n”, p-> id, p-> name); } int main() { Person a = { “Mike Smith”, 1234 }; print( a ); }
Background image of page 2
Dynamically allocating structures Structures can also dynamically allocated The “usual rules”: Call malloc() to request a memory region of the correct size Use sizeof to determine the correct size Cast the pointer returned by malloc to a pointer to the structure Free the region when the structure is no longer needed
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

13_pdfsam_cs2022_lectures - Pointers to structures Function...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online