0% found this document useful (0 votes)
50 views4 pages

Chapter 6 (B) - Arrays and Pointer (Part 2 - Pointers)

Pointers are variables that store the memory address of other variables. They are declared with a * and must match the data type of the variable being pointed to. Pointers can be assigned the address of another variable using the & operator. The * dereference operator is used to access the value at the address a pointer is pointing to. Pointer arithmetic allows pointers to be incremented or decremented to move through consecutive memory addresses like those of an array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views4 pages

Chapter 6 (B) - Arrays and Pointer (Part 2 - Pointers)

Pointers are variables that store the memory address of other variables. They are declared with a * and must match the data type of the variable being pointed to. Pointers can be assigned the address of another variable using the & operator. The * dereference operator is used to access the value at the address a pointer is pointing to. Pointer arithmetic allows pointers to be incremented or decremented to move through consecutive memory addresses like those of an array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Pointers

Chapter 6(b) -
Arrays and Pointers Variables that store the memory address
of other variables
- Pointers Variable name is prefixed by a ‘ * ’
(dereference operator)
Pointer’s data type must match the data
type of the variable it points to
Can be assigned the address of another
variable using the ‘&’(address of operator)
1 2

Example 1 Example 1 Expected Output


//declaring and initializing pointer
#include <iostream.h>
int main() Address of x: 0x0012FF7C
{
int x = 8, y = 16;
Address of y: 0x0012FF78
//declare and initialize a pointer Press any key to continue
int *x_ptr = &x; //address of variable x is assigned
//to the pointer x_ptr

int *y_ptr; //declare another pointer variable first


y_ptr = &y; //assign an address to this pointer later

//content of a pointer is the address of other variable


cout<<"Address of x: " << x_ptr << endl;
cout<<"Address of y: " << y_ptr << endl;
} 3 4

Getting Values using Pointers Example 2


//Accessing the value/data pointed by the pointer
#include <iostream.h>
 * dereference operator int main()
{
 If used in a variable declaration int x = 8;
 indicates that the variable being declared int *x_ptr; //declare a pointer
x_ptr = &x; //initialize a pointer
is a pointer
//shows the address of the variable x which is pointed by the pointer x_ptr
 If appears before a pointer variable cout<<"Address of the variable x: " << x_ptr << endl;
 references the data stored at the address
//shows the value of the variable x which is pointed by the pointer x_ptr
assigned to that pointer cout<<"Value stored in the variable x: " << *x_ptr << endl;
return 0;
}

5 6

Example 2 Expected Output Pointer Arithmetic

Address of the variable x: 0x0012FF7C A pointer variable can be reassigned


Value stored in the variable x: 8 another address or moved using arithmetic
Press any key to continue + + increment operator will move the
pointer along to the next address
- - decrement operator will move the
pointer along to the previous address
Larger jumps can be achieved using the
+ = and - = operators
7 8
Example 3 Example 3 Expected Output
//the pointer moves up one place, then again by a further one place,
before //jumping back down two places
#include <iostream.h>
int main() ptr address: 0x0012FF74 value: 1
{
int nums[ ] = {1, 2, 3}; //create an integer array ptr address: 0x0012FF78 value: 2
int *ptr = nums; //assigns the address of 1st array element ptr address: 0x0012FF7C value: 3
cout << "ptr address: " << ptr << " value: " << *ptr << endl;
ptr address: 0x0012FF74 value: 1
ptr++; //move pointer up one place( to the next address)
cout << "ptr address: " << ptr << " value: " << *ptr << endl; Press any key to continue
ptr++; //move pointer up one more place
cout << "ptr address: " << ptr << " value: " << *ptr << endl;

ptr -= 2; //move pointer down two places


cout << "ptr address: " << ptr << " value: " << *ptr << endl;
}
9 10

Pointers and Arrays Example 4


//A pointer is assigned the name of an array called intArr, which assigns the
address of //its first element to the pointer. A loop then increments the pointer
to each element
Pointer arithmetic is useful with arrays #include <iostream.h>
The elements in an array occupy consecutive int main()
memory places {
int intArr[10] = {1,2,3,4,5,6,7,8,9,10};
Assigning the name of an array to a
int *ptr = intArr; //shorthand for int *ptr = intArr[0];
pointer automatically assigns it the
for( int i = 0; i < 10; i++)
address of the first element (array) {
Incrementing the pointer by one moves the cout << "Element " << i;
cout << " value = " << *ptr <<endl;
pointer along the next element ptr++; //move pointer up one place( to the next address)
//i.e. points to the next array element
}
11
} 12

Example 4 Expected Output Changing Variable Values


Element 0 value = 1 The primary use of a pointer is to access
Element 1 value = 2 and, if appropriate, change the value of
Element 2 value = 3
the variable that the pointer is pointing to
Element 3 value = 4
Element 4 value = 5 Pointer can also be used to change the
Element 5 value = 6 value inside a variable
Element 6 value = 7 Use the variable pointer name preceded
Element 7 value = 8 by the * dereference operator to assign a
Element 8 value = 9 new value according to the appropriate
Element 9 value = 10
data type
Press any key to continue
13 14

Example 5
#include <iostream> Example 5 Expected Output
using namespace std;
int main ()
{
The value of num is 5
int num = 5; The value of num after num = 10 is 10
int* iPtr = &num;
The value of num after *iPtr = 15 is 15
cout << "The value of num is " << num << endl;
num = 10;
cout << "The value of num after num = 10 is " <<
num << endl;

*iPtr = 15;
cout << "The value of num after *iPtr = 15 is "
<< num << endl;
} 15 16
 The first change should be familiar, by the direct The placement of the dereferencing
assignment of a value to num: operator before a pointer is said to
num = 10. dereference the pointer.
 However, the second change is accomplished a Some texts refer to the indirection operator
new way, using the indirection operator:
as the dereferencing operator
*iPtr = 15;
The value of a de-referenced pointer is not
 The dereferencing operator is an asterisk, the
same asterisk that you used to declare the an address, but rather the value at that
pointer or to perform multiplication address—that is, the value of the variable
that the pointer points to.
17 18

Using a Variable Pointer to Point to Example 6


an Array
 The value of a pointer, even though it is an
address, is a numeric value.
 Therefore, you can perform arithmetic
operations on a pointer just as you can a
numeric value.
 The name of the array is a constant pointer
 Since you cannot change the value of the name
of an array, it being a constant pointer, you first
should declare a variable pointer and then
assign it to the address of an array
19 20

Example 6 Expected Output Passing Pointers to Functions

String are: One Two Three Four Passing a pointer to the original value of a
String are: First Two Three Last function allow the called function to
operate on the original value

21 22

Example 7
Example 7 Expected Output

num value is 5
num value is now 15

23 24
Example 8: Pointers to Pointers Example 8 Expected Output
int main()
{
 n = 44
int n=44; &n = 0x0064fd78
cout << " n = " << n << endl;
cout << " &n = " << &n << endl; pn = 0x0064fd78
int* pn=&n; // pn holds the address of n
cout << " pn = " << pn << endl;
&pn = 0x0064fd7c
cout << " &pn = " << &pn << endl; *pn = 44
cout << " *pn = " << *pn << endl;
int** ppn=&pn; // ppn holds the address of pn ppn = 0x0064fd7c
cout << " ppn = " << ppn << endl;
cout << " &ppn = " << &ppn << endl; &ppn = 0x0064fd80
cout << " *ppn = " << *ppn << endl; *ppn = 0x0064fd78
cout << "**ppn = " << **ppn << endl;
} **ppn = 44
25 26

You might also like