lecture07_4on1 - Memory Addresses When a program is...

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

View Full Document Right Arrow Icon
CS1010 Lecture 7 Pointers Henry Chia hchia@comp.nus.edu.sg Semester 1 2011 / 2012 Department of Computer Science School Of Computing National University Of Singapore CS1010 Lecture 7 – p.1/31 Lecture Outline Memory addresses Pointers Declaration and assignment Dereference Pointer comparison Pointers in functions Pointer parameters Address as return value Pointers to structures CS1010 Lecture 7 – p.2/31 Memory Addresses When a program is executed, memory locations are assigned to the variables. Each of these memory locations has a positive integer address that uniquely deFnes the location. When a variable is assigned a value, this value is stored in the corresponding memory location. The speciFc addresses used for the variables are determined each time that the program is executed and may vary from one execution to another. CS1010 Lecture 7 – p.3/31 Memory Addresses ±or the following declaration: int count = 1010; Detailed memory representation: count @100 1010 In the slides, a memory address value is indicated with a @ sign. Note: Addresses used for illustration are not representative of the exact address allocation policy. CS1010 Lecture 7 – p.4/31
Background image of page 1

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

View Full DocumentRight Arrow Icon
Address Operator The address of a variable can be referenced using the address operator , double x = 1.23; printf( "%lf stored at %p\n" , x, &x); Address may be printed with a %p placeholder. Apart from debugging with printf , actual address values are not used explicitly as they are system dependent and may vary from one execution to another. CS1010 Lecture 7 – p.5/31 Pointer Variable The C language allows us to store the address of a memory location in a special type of variable called a pointer variable (or simply pointer). When a pointer is deFned, the type of variable to which it will point must also be deFned. Thus, a pointer deFned to point to an integer variable cannot also be used to point to a ±oating-point variable. CS1010 Lecture 7 – p.6/31 Pointer Declaration Consider the following declarations: int count=1010, * ptr; // int count; // int * ptr; C uses an asterisk in declarations to indicate that the variable is a pointer variable. int * ptr ” is read from right to left as “declare ptr as a pointer to a int ”. IdentiFer name is “ ptr ”; type is “ int * ”. Although ptr can be initialized during declaration, use separate assignment statement to avoid notational confusion. CS1010 Lecture 7 – p.7/31
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/13/2011 for the course ELECTRICAL CS1010E taught by Professor Henrytan during the Spring '11 term at National University of Singapore.

Page1 / 8

lecture07_4on1 - Memory Addresses When a program is...

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

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