100 likes | 182 Views
Pointers. 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.
E N D
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.
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 nullxPtr = 0; // initialize to null
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 <<endl;*yPtr = 9;cin >> *yPtr; y 1A0 5 1A4 1A8 1AC yPtr 1A0 1B0 1B4 1B8 1BC
Using pointers to pass variables int main ( ){ void fun(float*); // prototypefloat var 10.0; fun(&var); // functioncallreturn 0; } // ******************void fun(float *fPtr) { *fPtr *= 2.54); } var 10.0 fPtr 1A4
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)
Two notations to pass arrays as parameters • Pointer notation • void fun(int *); // prototypeint Xray[max] = {10, 45, 82, 56, 83}; //declare arrayfun(Xray); // call the function • void fun(int *iPtr ) { for(int j=0; j<max; j++) *iPtr++ *= 2.54);} • Array notation • void fun(int [ ]); // prototypeint 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); }
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
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)
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