discussion7_no_ans

discussion7_no_ans - CS31: Introduction to Computer Science...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: CS31: Introduction to Computer Science I Discussion 7 May 13, 2011 TA: Paul Wais ( pwais@cs.ucla.edu ) Discussion Section 1L Information TA: Paul Wais ( pwais@cs.ucla.edu ) Office Hours: Thursdays 11:30am - 1:30pm in Boelter Hall 2432 Discusion Website: http://www.cs.ucla.edu/ pwais 1 Pointers Suppose we have a very, very large array of C-strings x[NUMCOLS + 1] containing millions of lines of text from Ashton Kutchers Twitter feed. Since Ashton writes very frequently, we must allocate additional rows to x in order to store new tweets. Furthermore, though Twitter currently limits Tweets to NUMCOLS = 140 characters, if Twitter were to perhaps increase the Tweet length limit to 1000 characters, we could no longer use x since we cant change NUMCOLS during runtime. In this case, what we need is a pointer to an arbitrary blob of memory where we can store Tweet characters. So far, we have discussed how to store data in variables and arrays . A variable has a value and an address . A variable int x = 5; has value 5 and some address &x . If we printed &x using cout , we would see something like 0x7fff5). An address is actually a number that refers to some byte in our computers memory; 0x7fff5 is just a fancy notation for this number. If we interpret our computers RAM as a giant array of bytes, then 0x7fff5 refers to the 0x7fff5th byte in this giant array. How can we refer to arbitrary locations in memory? Today, we will talk about pointers . We use pointers to store memory addresses . A pointer double * ptr; literally stores a number (i.e. address) to some memory location. The type of a pointer (i.e., in this case, double ) indicates that the pointer points to memory that should be interpreted as an double . The * character in the declaration double * ptr; indicates that ptr is a pointer. The * is also an operator that can be used for dereferencing (or following ) a pointer. Lets see a code example. 1 1 int main() 2 { 3 int x = 5; 4 int y = 10; 5 6 int * ptr; // An integer pointer 7 cout << "ptr : " << ptr << endl; // Uninitialized pointers point to arbitrary addresses 8 9 ptr = &x; // Point ptr at x 10 11 cout << "ptr : " << ptr << endl; // Print address stored in ptr 12 cout << "&x : " << &x << endl; // Print address of x 13 cout << " * ptr : " << * ptr << endl; // Print the value that ptr points to...
View Full Document

This note was uploaded on 07/06/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 7

discussion7_no_ans - CS31: Introduction to Computer Science...

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