Pointers and Arrays

Download Report

Transcript Pointers and Arrays

Pointers and Arrays
• There is a close connection between pointers and
array names.
double values[10];
double* pValue=values;
• The above will store the address of the array values
in the pointer pValue.
• The most significant difference between a pointer and
an array name is that you can modify the address
stored in a pointer, while the address that an array
name refers to is fixed.
Pointers and Arrays
tMyn
1
• You can perform operations on a pointer to alter the
address it contains.
• In terms of arithmetic operators, you’re limited to
addition and subtraction, but you can also compare
pointers to produce a logical result.
• Suppose you add 1 to a pointer defined above:
pValue++;
or
pValue += 1;
Pointers and Arrays
tMyn
2
• Pointer arithmetic implicitly assumes that the pointer
points to an array, and that the arithmetic operation is
on the address contained in the pointer.
• The compiler knows the number of bytes required to
store one element of the array, and adding 1 to the
pointer increments the address in the pointer by that
number of bytes.
• In other words, adding 1 to the pointer moves the
pointer to the next element in the array.
Pointers and Arrays
tMyn
3
double values[10];
Hexadesimal memory addresses
1000H
1008H
1010H
1018H
1020H
values[0]
values[1]
values[2]
values[3]
values[4]
pValue
(1000)
pValue+1 pValue+2
(1010)
(1008)
and so on...
values[5]
pValue+3
(1018)
Each element is assumed
to occupy 8 bytes
double* pValue=values;
1000
pValue
Pointers and Arrays
tMyn
4
• Using a pointer to store the address of a onedimensional array is relatively straightforward, but
with multidimensional arrays, things can get
complicated.
• You can usually do everything you need to do with
multidimensional arrays using array notation, and I
recommend that you stick to it whenever you can.
• First example deals with one-dimensional array of
type double:
Pointers and Arrays
tMyn
5
#include "stdafx.h"
#include <iostream>
using namespace System;
using namespace std;
int main(array<System::String ^> ^args)
{
double values[]={1.1, 2.2, 3.3};
double* pValues=values;
//Without pointer notation:
for(int i=0; i<3; i++)
cout<<values[i]<<" ";
cout<<endl;
Pointers and Arrays
tMyn
6
//Using pointer notation:
for(int i=0; i<3; i++)
cout<<*(pValues+i)<<" ";
cout<<endl;
//Array name by itself can behave like a pointer:
for(int i=0; i<3; i++)
cout<<*(values+i)<<" ";
cout<<endl;
return 0;
}
Pointers and Arrays
tMyn
7
• Second example deals with one-dimensional array of
type string:
Pointers and Arrays
tMyn
8
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace System;
using namespace std;
int main(array<System::String ^> ^args)
{
string names[3];
string* pNames=names;
for(int i=0; i<3; i++)
{
cout<<"Input a name: ";
getline(cin, names[i]);
}
Pointers and Arrays
tMyn
9
}
//Using array notation:
cout<<"The names given are:"<<endl;
for(int i=0; i<3; i++)
cout<<names[i]<<" ";
cout<<endl;
//Using pointer notation:
cout<<"The names given are:"<<endl;
for(int i=0; i<3; i++)
cout<<*(pNames+i)<<" ";
cout<<endl;
//Array name by itself can behave like a pointer:
cout<<"The names given are:"<<endl;
for(int i=0; i<3; i++)
cout<<*(names+i)<<" ";
cout<<endl;
return 0;
Pointers and Arrays
tMyn
10
Pointers and Arrays
tMyn
11
• Third example deals with two-dimensional array of
type int:
Pointers and Arrays
tMyn
12
#include "stdafx.h"
#include <iostream>
using namespace System;
using namespace std;
int main(array<System::String ^> ^args)
{
int numbers[3][3]={
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int* pNumbers1=&numbers[0][0];
int* pNumbers2=numbers[0];
Pointers and Arrays
tMyn
13
//Using array notation:
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
cout<<numbers[i][j]<<" ";
cout<<endl;
}
//Using pointer pNumbers1:
for(int i=0; i<9; i++)
{
cout<<*(pNumbers1+i)<<" ";
if(i==2 || i==5)
cout<<endl;
}
cout<<endl;
Pointers and Arrays
tMyn
14
//Using pointer pNumbers2:
for(int i=0; i<9; i++)
{
cout<<*(pNumbers2+i)<<" ";
if(i==2 || i==5)
cout<<endl;
}
cout<<endl;
}
//Array name by itself can behave like a pointer:
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
cout<<*(*(numbers+i)+j)<<" ";
cout<<endl;
}
return 0;
Pointers and Arrays
tMyn
15
Pointers and Arrays
tMyn
16