G x at locajon 0x04 y at 0x18 x originally 0x0 y

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: or ‘dereference’ Variable declaraJons   int x, y;   Finds two locaGons in memory in which to store 2 integers (1 word each)   Pointer declaraJons use *   int *ptr;   Declares a variable ptr that is a pointer to a data item that is an integer   Assignment to a pointer   ptr = &x;   Assigns ptr to point to the address where x is stored   (stores the address of x in ptr) Memory 28 University of Washington Addresses and Pointers in C   & = ‘address of value’ * = ‘value at address’ or ‘dereference’ To use the value pointed to by a pointer we use dereference (*)   Given a pointer, we can get the value it points to by using the * operator   *ptr is the value at the memory address given by the value of ptr   Examples   If ptr = &x then y = *ptr + 1 is the same as y = x + 1   If ptr = &y then y = *ptr + 1 is the same as y = y + 1   What is *(&x) equivalent to? Memory 29 University of Washington Addresses and Pointers in C   & = ‘address of value’ * = ‘value at address’ or ‘dereference’ We can do arithmeJc on pointers   ptr = ptr + 1; // really adds 4: type of ptr is int*, and an int uses 4 bytes!   Changes the value of the pointer so that it now points to the next data item in memory (that may be y, or it may not – this is dangerous!) Memory 30 University of Washington Assignment in C   Leo ­hand ­side = right ­hand ­side   LHS must evaluate to a memory LOCATION   RHS must evaluate to a VALUE (could be an address)   E.g., x at locaJon 0x04, y at 0x18   x originally 0x0, y originally 0x3CD02700 Memory 0000 00 00 00 00 0004 0008 000C 0010 0014 00 27 D0 3C 0018 001C 0020 0024 31 University of W...
View Full Document

This document was uploaded on 04/04/2014.

Ask a homework question - tutors are online