Pointers Introduction to pointers Pointer variables contain memory
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. • 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 *val. Ptr, val 1; int *x. Ptr; float *y. Ptr; • A pointer can only contain the address of that particular data type • Pointers should be initialized to 0 or null x. Ptr = 0; // initialize to null
Pointer operators & returns the address of its operand * returns the contents of the address it stores y 5 1 A 4 – Called the dereferencing operator • int y = 5; int *y. Ptr; y. Ptr = &y; • Examples of pointer use cout<< *y. Ptr <<endl; *y. Ptr = 9; cin >> *y. Ptr; 1 A 0 1 A 8 1 AC y. Ptr 1 B 0 1 B 4 1 B 8 1 BC 1 A 0
Using pointers to pass variables int main ( ) { void fun(float*); float var 10. 0; fun(&var); // return 0; } // prototype function call var 10. 0 // ********* void fun(float *f. Ptr) { *f. Ptr *= 2. 54); } f. Ptr 1 A 4
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 *); // prototype int Xray[max] = {10, 45, 82, 56, 83}; //declare array fun(Xray); // call the function • void fun(int *i. Ptr ) { for(int j=0; j<max; j++) *i. Ptr++ *= 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); }
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 x. Ptr; x. Ptr +=2; • If the address in x. Ptr 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 *a. Ptr; a. Ptr = a; – – Array name with subscripts a[3] Array name adding the offset (pointer arithmetic) *(a + 3) Pointer with subscripts a. Ptr[3] Pointer adding the offset *(a. Ptr + 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 *s. Ptr) – 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
- Slides: 10