Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.

Similar presentations


Presentation on theme: "CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel."— Presentation transcript:

1 CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel

2 lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g. x = 1.0; intarray[2] = 17; Constants and expressions are not lvalues

3 Memory and Data Representation Every lvalue is stored somewhere in memory and therefore has an address Once allocated, the address of an lvalue never changes, even though its contents may change Depending on type of data, different lvalues require different amounts of memory The address of an lvalue is itself data that can be manipulated and stored in memory

4 Array Arguments Creating a function that operates on an array is declaring a variable to contain an l-value Prototype: void f(int array[]); array is initialized with the memory address of the array passed in f(my_array); array then contains the address of an integer and using array[i] calculates an offset from this address

5 Pointer Declaration A pointer is a variable storing the address of another variable Declaration: type *ptr; Example: int *p1, *p2; p1 and p2 are referred to as pointers-to-int, and may contain the addresses of integers

6 Pointer Operations C++ defines two operators that manipulate pointer values: &address-of *value-pointed-to (dereference) Operand of & must be lvalue, and & returns the address of the lvalue Operand of * must be a pointer, and * returns the value pointed to by the pointer

7 Pass by Reference To modify value of argument, use pointer void SetToZero(int *ip) { *ip = 0; } which makes “ip” a pointer to the location of the value of the argument of the call SetToZero(&x);

8 Advantage of use of & operator When calling a function using func(x); you know the value of x will not change When calling a function using func(&x); the value of x may be changed by the function Easier to predict effects of function call

9 SwapInteger function When sorting arrays, we wanted to use SwapIntegers(array[low], array[high]); Using pointers allows the following: void SwapIntegers(int *p1, int *p2) { int temp = *p1; *p1 = *p2; *p2 = temp; } and SwapIntegers(&array[low], &array[high]);

10 Returning Multiple Results Convert time (minutes) to hours and minutes void Convert(int time, int *hours, int *mins) { *hours = time / 60; *mins = time % 60; } Call with ConvertTime(t, &h, &m);

11 Array Review Array elements are lvalues with addresses double list[3]; Three consecutive memory units, each 8 bytes long Each element has address, retrieved as &list[i] Address determined by &list[i]- base + i * 8

12 Pointer Arithmetic Addition and subtraction of pointers Fundamental insight of pointer arithmetic: –If p is a pointer to the initial element of an array arr, and k is an integer, then p + k is defined to be &arr[k] That is, adding an integer k to a pointer to an integer adds k * sizeof(int) to pointer

13 More Pointer Arithmetic If two pointers to integer p1 and p2 and an integer k have the relationship p2 == p1 + k Then p2 is exactly k * sizeof(int) larger than p1 and the result of subtracting the pointers p2 - p1 is the integer k

14 Recall ++ and -- Recall: x++ calculates value of x first and increments it. Expression’s value is value before increment. ++x increments value of x first and then uses new value as value of operation as a whole.

15 Incrementing pointers Consider the expression ( p is a pointer to int) *p++ Since unary operators are right-to-left order, thus it is equivalent to *(p++) and dereferences the pointer and returns the object to which it points, but, as a side effect, point p to the next integer

16 Pointers and Arrays The declaration int arr[5]; allocates space for an array of five integers The following identity holds arr  &arr[0] The function prototypes are equivalent double Average(int array[], int n); double Average(int *array, int n);

17 Proper Declaration Arrays and pointers may be used interchangeably General rule: –declare parameters to reflect their usage If using arrays, declare arrays If using pointers, declare pointers

18 Pointer/Array Difference The declaration int arr[5]; reserves memory for five integers The declaration int *p; only reserves memory for one pointer Declaring an array gives storage to work with Declaring pointer only lets you manipulate existing storage

19 Dynamic Memory Allocation Can use pointer to access additional memory (variables) and release it p1 = new int; p2 = new int[100]; Now can use pointers to access memory *p1 = 0; cout << p2[i]; –Can treat p2 as if it were the name of an array To release, use delete p1; delete []p2;

20 Special pointer NULL NULL is the value of a pointer that does not currently point to any data (usually value 0) Can not dereference a NULL pointer Used to see if allocate successful p2 = new int[100]; if (p2 == NULL) // error allocating


Download ppt "CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel."

Similar presentations


Ads by Google