Continuing with the previous post, today we will further study pointers and strings.
Pointer Expressions and Pointer Arithmetic:
Pointer arithmetic is of the following types:
–Increment/decrement
pointer (++ or --)
–Add/subtract
an integer to/from a pointer( +
or +=
, -
or -=)
–Pointers
may be subtracted from each other
but remember that pointer
arithmetic is meaningless unless performed on pointer to array
consider an example where there is a 5
element int array on a machine using 4 byte integer space.
–vPtr
points to first element v[
0 ], which is at location 3000
Vptr = v or vptr = &v[0]
–vPtr += 2;
sets vPtr
to 3008
vPtr
points to v[
2 ]
when subtracting
pointers (must both point to the same array, if not then LOGIC ERROR)
–Returns
number of elements between two addresses
vPtr2 = &v[ 2 ];
vPtr = &v[ 0 ];
vPtr2 - vPtr = 2
vPtr = &v[ 0 ];
vPtr2 - vPtr = 2
As far as pointer
assignment is concerned:
–Pointer can be assigned to another pointer if both are of same type
–If
not of the same type, cast operator must be used
–Exception:
Void Pointer: pointer to void
(type void
*)
Void Pointer: Pointer to void (type void *):
int
nValue
= 5;
void
*pVoid
= &nValue; //no need for casting
//
can not dereference pVoid
because it is a void pointer
int
*pInt
= static_cast<int*>(pVoid);
// cast from void* to int*
cout
<< *pInt
<< endl; // can dereference pInt
Void Pointer:
the code given below shows an example of void pointers:
void f (void *,void*,int);
main()
{
int x=9,y=5;
char r='w',t='z';
{
int x=9,y=5;
char r='w',t='z';
f(&r,&t,sizeof(r));
f(&x,&y,sizeof(x));
f(&x,&y,sizeof(x));
cout<<x<<endl;
cout<<r<<endl;
cout<<r<<endl;
}
void f(void* x, void* y, int sizeofptr)
{
{
int *nptr1, *nptr2;
char *nptr3, *nptr4;
char *nptr3, *nptr4;
if (sizeofptr == sizeof(int))
{ nptr1=(int* )x;
nptr2=(int* )y;
*nptr1=*nptr1**nptr2;}
else
{nptr3=(char* )x;
nptr4=(char* )y;
*nptr3=toupper(*nptr3);}
{ nptr1=(int* )x;
nptr2=(int* )y;
*nptr1=*nptr1**nptr2;}
else
{nptr3=(char* )x;
nptr4=(char* )y;
*nptr3=toupper(*nptr3);}
}
Relationship between Arrays and Pointers:
Arrays and pointers are closely related:
–
Array name is like a constant pointer
–
Pointers can do array subscripting operations:
int b[5];
int *bptr;
You can point ‘bptr’ to array ‘b’ using either;
bptr = b;
//remember name of array is constant pointer to address of the first location of array
Or
bptr = &b[0];
Array elements can easily be accessed with pointers
–
Element b[ n ] can be accessed by *( bPtr + n )
•
Called pointer/offset notation
•
‘n’ is the offset
•
DO NOT forget the brackets, since * bPtr +
n will simply add n to *bPtr
–
Accessing array addresses with pointers:
•
&b[ 3 ] same as bPtr + 3
–
Array name can be treated as pointer;
•
b[ 3 ] same as *( b + 3 )
•
This is possible since name of the array is a
pointer itself.
–
Pointers can be subscripted (pointer/subscript
notation)
•
bPtr[ 3 ] same as b[ 3 ]
Array of Pointers:
An array can contain pointers
–This is commonly
used to store array of strings
char *suit[ 4 ] =
{"Hearts", "Diamonds",
"Clubs", "Spades" };
"Clubs", "Spades" };
–Each
element of suit
points to char
*
(a string)
–Array
does not store strings, only pointers to strings
–suit
array has fixed size, but strings can be of any size
Post a Comment