/
Pointers Dr. Jose Annunziato Pointers Dr. Jose Annunziato

Pointers Dr. Jose Annunziato - PowerPoint Presentation

norah
norah . @norah
Follow
64 views
Uploaded On 2024-01-13

Pointers Dr. Jose Annunziato - PPT Presentation

Variable Addresses Variable declarations allocate enough bytes to hold the variable type Each byte has a particular address The address of the variable is the address of the first byte allocated for the variable ID: 1039542

float balance ptr int balance float int ptr size amp address variable cout array pointer pointers function endl salary

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Pointers Dr. Jose Annunziato" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. PointersDr. Jose Annunziato

2. Variable AddressesVariable declarations allocate enough bytes to hold the variable type. Each byte has a particular addressThe address of the variable is the address of the first byte allocated for the variableConsider the following variables char letter; short number; float amount;Address operator & gets a variable's addressletternumberamount

3. Address OperatorThe & operator returns the address of a variableThe sizeof() function returns the number of bytes float balance = 250.75; cout << "Balance = " << balance << endl; cout << "Address of balance: " << &balance<<endl; cout << "Size balance: " << sizeof(balance) << endl;

4. Pointer VariablesPointer variables, or pointers, can hold the address of another variableUse the asterisk, *, after a data type to declare a pointer to a variable of that type float *balance; // balance points to a float int *type; // type points to an int The asterisk can be on the type instead of variable float* balance; // it's the same. Emphasizes int* type; // pointer to type

5. Creating and Using PointersUse the dereference operator, *, on a pointer to operate on the variable pointed to float balance = 5000.0; float* ptr; ptr = &balance; cout << balance << endl; cout << *ptr << endl; // access original variable *ptr = 6000.0; // change original variable cout << *ptr << endl;

6. Using PointersPointers can point to various variables float balance = 500.0, float amount = 600.0; float* ptr; ptr = &balance; *ptr += 100; // depositing 100 ptr = &amount; *ptr = *ptr * 1.05; // adding 5% interest

7. Arrays and PointersArrays are already pointersArray names refer to the first elementArray indices compute address of elements based on address of first elementIndices advance as many bytes as the type of each element requires float balance[3];balance[0]balance[1]balance[2]balance

8. Pointer ArithmeticWe can address array elements with pointer arithmetic float balance[] = {10, 20, 30, 40}; cout << balance << endl; // address of 1st cout << *balance << endl; // balance[0] ptr = balance; // notice not &balance ptr + 1 // &balance[1] ptr + 2 // &balance[2] *(ptr + 3) // balance[3]

9. Pointer Arithmetic float balance[] = {10, 20, 30, 40}; *balance == balance[0] *(balance + 1) == balance[1] *(balance + 2) == balance[2] *(array + index) == balance[index]balance[0]balance[1]balance[2]balance*balance*(balance+1)*(balance+2)

10. Iterating Over Arrays with Pointers float balance[] = {1,2,3,4}; float* ptr = balance; for ( int k = 0; k < 4; k++ ) { 1,2,3,4, cout << *ptr << ", "; ptr++; } for ( int k = 0; k < 4; k++ ) { 4,3,2,1, ptr--; cout << *ptr << ", "; }

11. Comparing PointersWhen comparing pointers, we are comparing addressesUse relational operators ==, !=, >, >=, <, <= to compare relations between two pointersTo compare the values the pointers refer to, deference the pointers to compare the original values; int a = 10, b = 10; int* ptr1 = &a, ptr2 = &b; ptr1 == ptr2 ? … : …; *ptr1 == *ptr2 ? … : …;

12. Pointers as Function ParametersPointers allow passing by reference and manipulating the original variable void giveRaise ( float* stipend, float raise ) { *stipend += raise; } int main() { float salary = 10000.0; giveRaise ( &salary, 1000.0 ); cout << salary << endl; }

13. Pointers as Function ParametersUse the & operator for easier pass by reference void giveRaise ( float &stipend, float raise ) { stipend += raise; } int main() { float salary = 10000.0; giveRaise ( salary, 1000.0 ); cout << salary << endl; }

14. Note on Parameter NamesParameter names are local variables, their names don’t have to match variable being passed in void giveRaise ( float* stipend, float raise ) { *stipend += raise; } int main() { float salary = 10000.0; giveRaise ( &salary, 1000.0 ); }

15. Note on Parameter NamesWhen forward declaring a function, the name of the parameter is optional since it's not going to be used yetIt's, of course, mandatory in the function definition void giveRaise ( float *, float ); int main() { float salary = 10000.0; giveRaise ( &salary, 1000.0 ); } void giveRaise ( float* stipend, float raise ) { *stipend += raise; }

16. Passing Arrays to FunctionsAs we saw earlier, pointers and arrays are closely related and array names are pointers to 1st elementFunction parameters accepting arrays can be declared as pointers instead of arrays: float average ( float values[], int size ) { … }Is equivalent to: float average ( float *values, int size ) { … }Manipulate array in function using array indices or pointer arithmetic. It's the same

17. Dynamic Memory AllocationPointers allow allocating memory space at run timeUse the new operator to allocate new spaceThe new memory space is accessible by the address to the 1st byte of the data structure int* intPrt; // will hold the address intPtr = new int; // of the new data *intPtr = 25; // dereference to modify cout << *intPtr; cin >> *intPtr; total += *intPtr;

18. Deallocating MemoryMemory that has been allocated with new can be deallocated using delete int* ptr = new int; delete ptr;Do not deallocate memory that has not been allocated with new

19. Array Sizes are ConstantsA common use of dynamic memory allocation is to create arrays of an unknown sizeDeclared array sizes must be constants const int SIZE = 100; float balances [ 100 ]; float amounts [ SIZE ];You can not use a variable as the size of a declared array int size; cin >> size; float balances [ size ];

20. Allocating Dynamic ArraysArrays can be allocated dynamically using the new operator and assigning the address to a pointer int size; cin >> size; float* amounts = new float [ size ];You can then use regular array index syntax: amounts [ 0 ] = 123.23; amounts [ 1 ] = 234.56;

21. Returning Pointer from FunctionsFunctions can create dynamically allocated structures and return pointers to them float* createFloatArray ( int size ) { float* floats = new float [ size ]; return floats; }You can then use the pointer as an array int length; cin >> length; float* balances = createFloatArray ( length ); balances [ 0 ] = 234.56;

22. Careful Returning Out of Scope StructuresWhen returning structures allocated in a function, careful it is not a structure that will be destroyed when the function returns string* createStringArray () { string descriptions [ 100 ]; return descriptions; }Above, the descriptions array is a local variable and will be destroyed when the function returns

23. Returning Pointer from FunctionsThis function works because it returns the address of the dynamically allocated structure string* createStringArray ( int size ) { string* strings = new string [ size ]; return strings; }Above, even though the strings variable is destroyed when the function terminates, the value is returned as an address that can be used to retrieve the original arrayThe array still exists since delete has not be called

24. DynamicArraysDemo.cpp