Download presentation
Presentation is loading. Please wait.
Published byNeal Kelly Modified over 9 years ago
1
Pointers
2
Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value. But a pointer contains the address of a variable that contains a specific value. A variable name directly references a value and a pointer indirectly references a value. Referencing a value through a pointer is called indirection.
3
Pointer variable declarations A pointer is declared to contain the address of a certain data type int *valPtr, val1; int *xPtr; float *yPtr; A pointer can only contain the address of that particular data type Pointers should be initialized to 0 or null xPtr = 0; // initialize to null
4
Pointer operators & returns the address of its operand * returns the contents of the address it stores –Called the dereferencing operator int y = 5; int *yPtr; yPtr = &y; Examples of pointer use cout > *yPtr; 1A0 1A4 1A8 1AC 1B0 1B4 1B8 1BC y yPtr 5 1A0
5
Using pointers to pass variables int main ( ) { void fun(float*); // prototype float var 10.0; fun(&var); // function call return 0; } // ****************** void fun(float *fPtr) { *fPtr *= 2.54); } var 10.0 fPtr1A4
6
Pointers and arrays The compiler treats an array name as a pointer to the first byte of the array This address (the first byte of the array) is a constant and cannot be changed by the user The data in the array can be changed, just not the pointer to the first byte (the array name)
7
Two notations to pass arrays as parameters Pointer notation void fun(int *); // prototype int Xray[max] = {10, 45, 82, 56, 83}; //declare array fun(Xray); // call the function void fun(int *iPtr ) { for(int j=0; j<max; j++) *iPtr++ *= 2.54);} Array notation void fun(int [ ]); // prototype int Xray[max] = {10, 45, 82, 56, 83}; fun(Xray); // call the function void fun(int A[ ] ) { for(int j=0; j<max; j++) A[j] *= 2.54); }
8
Pointer arithmetic When a pointer is incremented, it is incremented by the size of the object to which it points –decrement also When an integer is added to a pointer, it is multiplied by the size of the object to which it points. The legal operators for pointer arithmetic are ++ or – + or + = - or - = Example: int xPtr; xPtr +=2; If the address in xPtr is 1000 it will be 1008 after the instruction since an integer occupies four bytes
9
The relationship between pointers and arrays An array name can be thought of as a constant pointer –You can refer to an element of an array using pointer arithmetic with the array name (add the offset) Pointers can be used to do any operation involving array subscripting –You can refer to an element of an array using a subscript with the pointer You have four ways to refer to the elements of an array int a[5] = {15,3,78,45,65}; int *aPtr; aPtr = a; –Array name with subscripts a[3] –Array name adding the offset (pointer arithmetic) *(a + 3) –Pointer with subscripts aPtr[3] –Pointer adding the offset *(aPtr + 3)
10
Using const with pointers and data Four ways (read left to right) –A constant pointer to constant data –const int *const ptr = &x; –A non-constant pointer to constant data –const int *ptr –void fun(const char *sPtr) –A constant pointer to non-constant data –int * const prt = &x; –A non-constant pointer to non-constant data Always use the principal of least privilege –Give a function enough access to the data in its parameters to accomplish it task, but no more
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.