tutorial2 - CSCC69 Tutorial 2 Dan Zingaro May 10, 2010...

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

View Full Document Right Arrow Icon
CSCC69 Tutorial 2 Dan Zingaro May 10, 2010
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 I Every variable has a memory address I To get the address of a variable, use the “address-of” operator I Pointers are variables that store memory addresses I To get the value referred to by a pointer, use the “dereference” operator * int x = 42; int* x_ptr = &x; int* heap_ptr = malloc (sizeof (int)); int y = *x_ptr; // 42 int z = *heap_ptr; // Undefined!
Background image of page 2
NULL I If you have no useful value for a new pointer, initialize it to NULL I Functions that return pointers usually return NULL to signify an error (e.g. malloc ) I We should check return values to make sure they aren’t NULL if ((p = malloc (sizeof (int))) == NULL) perror ("Error: out of memory.\n");
Background image of page 3

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

View Full DocumentRight Arrow Icon
Pass-by-Value/Reference I If variables are passed to a function by value, any changes made will not be seen outside of the function I Each variable is copied to the function’s stack, so changes are made only to this copy I To pass by reference, pass pointers to values rather than the values themselves I The function can then change the value referenced by the
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 16

tutorial2 - CSCC69 Tutorial 2 Dan Zingaro May 10, 2010...

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

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