85_pdfsam_cs2022

85_pdfsam_cs2022 - Pointers to structures Function calls...

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
Structures and the heap So far we’ve been storing structures on the stack Structures can also be stored on the heap 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 neededf
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.

This note was uploaded on 01/10/2010 for the course CS 2022 at Cornell.

Page1 / 6

85_pdfsam_cs2022 - Pointers to structures Function calls...

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