Another Example
#include <iostream.h>
int main(){
char* stringptrs[] = {"Live", "long", "and", "prosper"};
char** sptr = stringptrs;
cout << "sptr is a pointer to an array of pointers..." << endl << endl;
for(int i=0; i<4; i++, sptr=stringptrs+i){
cout << "The address of sptr is ... " << &sptr << endl;
cout << "The contents of sptr are ... " << sptr << endl;
cout << "*sptr dereferences to give ... " << *sptr << endl;
cout << "**sptr dereferences to give ... " << **sptr << endl;
(*sptr)++;
while( **sptr){
cout << "(*sptr)++ = " << *sptr << endl;
cout << "**sptr = " << **sptr << endl;
(*sptr)++;
}
cout << endl << endl;
}
return(0);
}
Output:
sptr is a pointer to an array of pointers...
The address of sptr is ... 0xf7fff924
The contents of sptr are ... 0xf7fff928
*sptr dereferences to give ... Live
**sptr dereferences to give ... L
(*sptr)++ = ive
**sptr = i
(*sptr)++ = ve
**sptr = v
(*sptr)++ = e
**sptr = e
The address of sptr is ... 0xf7fff924
The contents of sptr are ... 0xf7fff92c
*sptr dereferences to give ... long
**sptr dereferences to give ... l
(*sptr)++ = ong
**sptr = o
(*sptr)++ = ng
**sptr = n
(*sptr)++ = g
**sptr = g
The address of sptr is ... 0xf7fff924
The contents of sptr are ... 0xf7fff930
*sptr dereferences to give ... and
**sptr dereferences to give ... a
(*sptr)++ = nd
**sptr = n
(*sptr)++ = d
**sptr = d
The address of sptr is ... 0xf7fff924
The contents of sptr are ... 0xf7fff934
*sptr dereferences to give ... prosper
**sptr dereferences to give ... p
(*sptr)++ = rosper
**sptr = r
(*sptr)++ = osper
**sptr = o
(*sptr)++ = sper
**sptr = s
(*sptr)++ = per
**sptr = p
(*sptr)++ = er
**sptr = e
(*sptr)++ = r
**sptr = r