Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.

Similar presentations


Presentation on theme: "CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm."— Presentation transcript:

1 CS 31 Discussion, Week 7 Faisal Alquaddoomi, faisal@cs.ucla.edufaisal@cs.ucla.edu Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm

2 C-Strings Review C-Strings are just char arrays, with some convenient functions for manipulating them – End in a ‘nul-terminator’ that signifies the end of the string C-Strings have special manipulation functions in the library

3 2D Arrays Review 2D arrays are like 1D arrays, except they have an extra dimension – by convention, the first dimension is the row, the second is the column – declare: int myArray[5][10]; // 5 rows, 10 columns – access/change: myArray[2][3] = 888; // ^ item at row 2, col 3 is now 888 2D arrays can be statically allocated, but the second dimension (columns) must be specified Similarly, when writing a function that takes a 2D array as a parameter, the second dimension must also be specified: void myFunc(char inArray[][5]) { /* body of function here… */ }

4 Notes on Project 5 Just like any other type, you can have 2D arrays of C-strings, e.g. char doc[50][81]; // 50 lines, 80 chars + ‘\0’ each Reading lines from istream is the same as from cin Here’s a way to read 50 lines from an istream& (say, ‘mystream’): int lines = 0; while (lines < 50 && mystream.getline(doc[lines++],80)) continue;

5 Variables Revisited Variables have a type, a name, and a value Arrays are extensions of this idea We can pass variables “by value” to functions, which causes their contents to be copied We can also pass “by reference”, which changes the original variable when the reference changes Arrays are always passed by reference…but why?

6 How Variables Are Stored int main() { int x; int y = 2; char c; double d; int q[5] = { 1, 2, 3 }; x = 3; c = ‘H’; d = 3.5; cout << “x: ” << x << endl; return 0; }

7 How Variables Are Stored int main() { int x; int y = 2; char c; double d; int q[5] = { 1, 2, 3 }; x = 3; c = ‘H’; d = 3.5; cout << “x: ” << x << endl; return 0; } 32‘H’3.5123?? xycdq[0]q[1]q[2]q[3]q[4] name: value:

8 Type Sizes (For 32-bit Systems) NameSizeRange char1 byte signed: -128 to 127 unsigned: 0 to 255 short int2 bytes-32768 to 32767 int4 bytes -2147483648 to 2147483647 bool1 bytetrue or false float4 bytes+/- 3.4e +/- 38 (~7 digits) double8 bytes+/- 1.7e +/- 308 (~15 digits)

9 Variable Storage, Scaled to Size 32H3.5 xycd name: value: 1 q[0] 2 q[1] 3 q[2] ? q[3] ? q[4] name: value: 4 bytes

10 One More Attribute for Variables In addition to a type, a name, and a value, variables also have an address Just like the address of a house, it specifies where to find that variable in memory – Arrays are kind of like apartments, where each slot specifies a different place, all starting from the same address x @ 0 y @ 4 z @ 8 q @ 12 to 32 …

11 Variables with Addresses 32H3.5 xycd name: value: 1 q[0] 2 q[1] 3 q[2] ? q[3] ? q[4] name: value: 4 bytes 012345678910111213141516 17181920212223242526272829303132333435..

12 Operations on Addresses From the previous example: – x has address 0, y has address 4, c has address 8, d is at 9 – q starts @17 and has slots @17, 21, 25, 29, and 33 Retrieve the address of a variable/array using the ‘&’ operator, e.g.: cout << (&x); // might print ‘0’ Get the value at an address using the ‘*’ operator, e.g.: cout << *(&x); // would print 3

13 Storing an Address, “Pointers” Even though addresses are just numbers, they are very often stored in a special family of types – called the “pointer types”; an instance of this type is a “pointer” For instance, the address of an int can be stored in a pointer type called int*, read as “pointer to int” – one pointer type per regular type, e.g. int has “int*”, char has “char*”, etc. – There’s a special pointer type called “void*”, but we’ll get to that later…

14 Creating and Assigning a Pointer Assuming the previous code, this creates a pointer to ‘x’ and manipulates it a bit: // stores address of x in px int* px = &x; // prints address of x cout << (int)(px) << endl; // changes x to 32 *(px) = 32; 32‘H’3.5 xycd name: value: 0 px …

15 Pointers to Arrays (and Pointer Arithmetic) There is no difference between a pointer to a variable and a pointer to an array – …but this is where the type of the pointer starts to become useful Set ‘pq’ to point to our ‘q’ array: int* pq = q; // q was already a pointer! // …q[0] is the value of the first element Move ‘pq’ to the next element in ‘q’: pq = pq + 1; // moves pq 4b forward ‘pq’ moves forward by 4 bytes because it’s a pointer to int, which is of size 4 – if it were a pointer to char, we’d move forward 1 byte and be in the middle of the first element rather than at the second element

16 Array Access Revisited As established, ‘q’ is actually a pointer that points to the first element in its array – This is why we’ve had to pass the length of an array around all the time, because all ‘q’ knows is that it starts at some place and has a type – ‘q’s value tells it where to start, and its type tells it how to get to a particular element’s offset (e.g. by multiplying size of type and desired element position, then adding it to the address of the first element, which is ‘q’) The operator q[x] is actually *(q + size of type * index), e.g.: // let’s get the third element, slot 2 cout << *(q + 4*2); // 4 == size of int in bytes // gets the value of the element // starting at q, then shifted over ‘x’ // units of the type’s size

17 Passing by Reference Revisited The ‘reference’ that’s passed to a function is actually the address of the variable C++ is doing you some confusing ‘favors’, like using ‘&’ on the argument and implicitly using ‘*’ every time the reference is mentioned – added to the language because passing pointers is so common

18 Passing by Reference, Part 2 This code: void p(int& x) { x = 32; } int main() { int a; p(a); cout << a; /*etc*/ } …is (almost) equivalent to this code: void p(int* x) { *x = 32; } int main() { int a; p(&a); cout << a; /*etc*/ }

19 Addresses and Pointers Review All variables have an address in memory (as well as an amount of space they consume dictated by their type) The address can be retrieved via the ‘&’ operator The value at an address can be retrieved/changed using the ‘*’ operator on an address

20 Addresses and Pointers Review, Part 2 A pointer is a special type used for holding addresses Pointers can be manipulated using +, -, which moves them in ‘steps’ of their type’s size, e.g.: int* p; p = p + 3; // moves p (3*4) = 12 bytes forward in memory Arrays are actually pointers to the first element – The subscript operator [] is implemented using pointer arithmetic; assuming q is an array of ints: q[x] == *(q+4*x)


Download ppt "CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm."

Similar presentations


Ads by Google