w3-one - 198:211 Computer Architecture Week 3 Fall 2012 l Topics l l l Pointers Advanced Types I\/O Quick Note on Memory l l l l l l l Well get into much

w3-one - 198:211 Computer Architecture Week 3 Fall 2012 l...

This preview shows page 1 - 13 out of 72 pages.

198:211 Computer Architecture l Topics: l Pointers l Advanced Types l I/O Week 3 Fall 2012
Image of page 1
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)
Image of page 2
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
Image of page 3
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
Image of page 4
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);
Image of page 5
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
Image of page 6
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
Image of page 7
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)
Image of page 8
Pointer Example l int i; l int *ptr; l i = 4; l ptr = &i; l *ptr = *ptr + 1; i ptr 4300 4304 random address ? ? ?
Image of page 9
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 ? ?
Image of page 10
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 ?
Image of page 11
Pointer Example l int i; l int *ptr; l i = 4; l ptr = &i; l
Image of page 12
Image of page 13

You've reached the end of your free preview.

Want to read all 72 pages?

  • Fall '08
  • Staff
  • Array, Pointer, scanf, l༆ 

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes