Memory and Pointers

Memory and Pointers - 2 Get memory address from another...

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

View Full Document Right Arrow Icon
Memory and Pointers A pointer is a variable that contains an address in memory. In a 32 bit architectures, the size of a pointer is 4 bytes independent on the type of the pointer. 0 (4GB-1) 232-1 Address space p:20 : 12 Char c = ‘A’; //ascii 65 char * p = &c; c:12 : 65
Background image of page 1

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

View Full DocumentRight Arrow Icon
Ways to get a pointer value 1. Assign a numerical value into a pointer Char * p = (char *) 0x1800; *p = 5; // Store a 5 in location 0x1800; Note: Assigning a numerical value to a pointer isn't recommended and only left to programmers of OS, kernels, or device drivers
Background image of page 2
Ways to get a pointer value
Background image of page 3

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

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

Unformatted text preview: 2. Get memory address from another variable: int *p; int buff[ 30]; p = &buff[1]; *p =78; buff[0]:100 : buff[1]:104 : buff[29]:216 : 220 : P: 96: 104 78 Ways to get a pointer value 3. Allocate memory from the heap int *p p = new int; int *q; q = (int*)malloc(sizeof(int)) Ways to get a pointer value • You can pass a pointer as a parameter to a function if the function will modify the content of the parameters void swap (int *a, int *b){ int temp; temp=*a; *a=*b; *b=temp; } In main: swap(&x, &y)...
View Full Document

This note was uploaded on 01/30/2012 for the course CS 252 taught by Professor Gustavorodriguez during the Fall '11 term at Purdue.

Page1 / 5

Memory and Pointers - 2 Get memory address from another...

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