198:211 Computer Architecture l Topics: l Pointers l Advanced Types l I/O Week 3 Fall 2012
Quick Note on Memory… l We ’ ll get into much more detail on this later in the course l Remember Von Neuman Architecture l All data is stored in memory l Memory is addressable l Think of lockers!! l Locker number, contents of locker l Amount of memory required per data type varies Output Unit Input Unit Memory Unit Control Unit Arithmetic and Logic (ALU)
Memory l Think of memory as a long continuous array l Memory allocated based on bytes l Has an address and contents associated with that address 1 2 3 4 5 6 7 8 9 10 11 12 13
Memory l Memory allocated to variables in bytes l int x; l double y; l char z; l short int w; l This memory stays allocated for the lifetime of the variable 1 2 3 4 5 6 7 8 9 10 11 12 13
Pointers - Introduction l Any variable that is declared within a program is assigned to a portion of memory to store it ’ s assigned value. l Run time systems maintains a mapping (called a symbol table ) that store, for each variable, its assigned location in memory. l In C, address of a variable can be access by & operator ‘ a ’ ‘ b ’ ‘ c ’ x3100 x3101 x3102 address value char1 char2 x3101 x3100 VarName MemLoc char char1 = ‘ a ’ char char2 = ‘ b ’ ; printf (… char1,&char1);
Pointers - Intro (continued) l Since we can talk about memory address, we should be able to assign it to a variable as well! l A Pointer is a type of variable that stores the address of another variable in memory l Allows us to indirectly access variables l in other words, we can talk about its address (or where its stored) rather than its value
Pointers in C l C lets us talk about and manipulate pointers as variables and in expressions. l Declaration l int *p; /* p is a pointer to an int */ l A pointer in C is usually a pointer to a particular data type: int* , double* , char*, float* , etc. l Pointer Operators l *p -- returns the value pointed to by p l &z -- returns the address of variable z
Pointers in C l Declaration l int date; l date=5; /* date is integer with value 5 */ l int *p; /* p is a pointer to an integer variable */ l p=&date /* value of p is address of date */ l Remember p is of type pointer (think address) l Remember *p is of type integer (think value) l Initialize pointer l print (… ,*p); /*STMT2 prints what? */ l *p = date + 1 /* STMT 2 make contents of p same as integer date + 1 */ 5 10 a p Address=10 Address=6 Address=2 printf ( “ … format … ” , a, &a, *p, p, &p)
Pointer Example l int i; l int *ptr; l i = 4; l ptr = &i; l *ptr = *ptr + 1; i ptr 4300 4304 random address ? ? ?
Pointer Example l int i; l int *ptr; l i = 4; l ptr = &i; l *ptr = *ptr + 1; store the value 4 into the memory location associated with i i ptr 4300 4304 random address 4 ? ?
Pointer Example l int i; l int *ptr; l i = 4; l ptr = &i; l *ptr = *ptr + 1; store the address of i into the memory location associated with ptr i ptr 4300 4304 random address 4 4300 ?
Pointer Example l int i; l int *ptr; l i = 4; l ptr = &i; l
You've reached the end of your free preview.
Want to read all 72 pages?
- Fall '08
- Array, Pointer, scanf, l༆