Date post: | 01-Jun-2018 |
Category: |
Documents |
Upload: | jeff-hardy |
View: | 223 times |
Download: | 0 times |
of 25
8/9/2019 Pointers.pptx(1)
1/25
Pointers
8/9/2019 Pointers.pptx(1)
2/25
Getting the Address of a Variable
} Each variable in program is stored at a unique
address
}
Use address operator& to get address of avariable:int num = -23;
cout
8/9/2019 Pointers.pptx(1)
3/25
Pointer Variables
}Pointer variable pointer): variable that holds an
address
} Can perform some tasks more easily with anaddress than by accessing memory via a symbolic
name:} Accessing unnamed memory locations
} Array manipulation
8/9/2019 Pointers.pptx(1)
4/25
Pointer Variables
} Definition:int *intptr;
} Read as:
“intptr can hold the address of an int”
} Spacing in definition does not matter:int * intptr;
int* intptr;
8/9/2019 Pointers.pptx(1)
5/25
Pointer Variables
} Assignment:int num = 25;int *intptr;intptr = #
}
Memory layout:
Can access num using intptr and indirection
operator *: cout
8/9/2019 Pointers.pptx(1)
6/25
Indirection Operator (dereferencing)
}
Value of a variable pointed to by ptr } Achieved by * operator
int num = 25;
int *intptr;intptr = #
cout
8/9/2019 Pointers.pptx(1)
7/25
Pointers & Arrays
8/9/2019 Pointers.pptx(1)
8/25
} Array name is starting address of array
int a[] = {4, 7, 11};
cout
8/9/2019 Pointers.pptx(1)
9/25
Example
void main (){
int a[ ] = {4, 7, 11};
int *ptr = a;
cout
8/9/2019 Pointers.pptx(1)
10/25
Pointers in Expressions
Given:
int a[]={4,7,11};
int *ptr = a;
cout
8/9/2019 Pointers.pptx(1)
11/25
Array Access
} Array notation
a[i]
is equivalent to the pointer notation
*(a + i)
8/9/2019 Pointers.pptx(1)
12/25
Pointer Arithmetic
8/9/2019 Pointers.pptx(1)
13/25
Pointer Arithmetic
} Some arithmetic operators can be used with pointers:
- Increment and decrement operators ++, --
- Integers can be added to or subtracted from
pointers using the operators +, -, +=, and -=
- One pointer can be subtracted from another
by using the subtraction operator -
8/9/2019 Pointers.pptx(1)
14/25
Pointer arithmetic
} Integer math operations can be used with pointers.
} If you increment a pointer, it will be increased by the
size of whatever it points to.
a[0] a[1] a[2] a[3] a[4]
int *ptr = a;
*ptr
*(ptr+2)*(ptr+4)
8/9/2019 Pointers.pptx(1)
15/25
Pointer Arithmetic (++/--)
} Assume the variable definitions
int a[]={4,7,11};
int *ptr = a;
} Examples of use of ++ and –- ptr++; // points at 7
ptr--; // now points at 4
8/9/2019 Pointers.pptx(1)
16/25
Pointer Arithmetic (+)
• Assume the variable definitions:
int a[]={4,7,11};
int *ptr = a;
• Example of use of + to add an int to a pointer:
cout
8/9/2019 Pointers.pptx(1)
17/25
Pointer Arithmetic (+=)
• Assume the variable definitions:
int a[]={4,7,11};
int *ptr = a;
• Example of use of +=:
ptr = a; // points at 4
ptr += 2; // points at 11
8/9/2019 Pointers.pptx(1)
18/25
Pointer Arithmetic
} Assume the variable definitions
int a[] = {4,7,11};
int *ptr = a;
} Example of pointer subtraction ptr += 2;
cout
8/9/2019 Pointers.pptx(1)
19/25
Comparing Pointers
}
Relational operators can be used to compareaddresses in pointers
} Comparing addresses in pointers is not the same as
comparing contents pointed at by pointers: if (ptr1 == ptr2) // compares // addresses
if (*ptr1 == *ptr2) // compares // contents
8/9/2019 Pointers.pptx(1)
20/25
Pointers & Functions
8/9/2019 Pointers.pptx(1)
21/25
Pointers as Function Parameters
} A pointer can be a parameter
} Works like a reference parameter to allow change toargument from within function
} A pointer parameter must be explicitly dereferencedto access the contents at that address
8/9/2019 Pointers.pptx(1)
22/25
Pointers as Function Parameters
} Requires:
1) asterisk * on parameter in prototype and
heading void getNum(int *ptr);
2) asterisk * in body to dereference the pointer
cin >> *ptr;
3) address as argument to the function getNum(&num);
8/9/2019 Pointers.pptx(1)
23/25
Pointers as Function Parameters
void swap(int *x, int *y)
{
int temp;
temp = *x; *x = *y;
*y = temp;
}
int num1 = 2, num2 = -3; swap(&num1, &num2);
8/9/2019 Pointers.pptx(1)
24/25
Example (returning search key index)
int find(int *p, int key)
{
for(int i=0;i
8/9/2019 Pointers.pptx(1)
25/25
Contd…
void main ()
{
int a[]={1,12,45,63,18,14,6,9,10,7};
int num;
coutnum;
int *ptr=a;
int k=find ptr,num);
if (k!=-1)
cout