Pointers and Strings in C++

An array of pointers makes more efficient use of available memory by consuming lesser number of bytes to store the strings. An array of pointers make more the manipulation of the strings much easier.

 

One can easily exchange the positions(from one to another) of strings(group of characters) in the array using pointers without actually touching their memory locations.

Example....

#include<iostream.h>

#include<conio.h>

#include<string.h>

void main()

{

clrscr();

char *names[]={"Rekha","Ajay","Sunil","Anil","sachin"};

int len=0;

len=strlen(names[1]);

cout<<"\n originally string 2 is";

cout.write(names[1],len).put('\n');

cout<<"and string is";

cout.write(names[3], len).put('\n');

char *t;

t=names[1];

names[1]=names[3];

names[3]=t;

len=strlen(names[1]);

cout<<"exchange string 2. is";

cout.write(names[1],len).put('\n');

cout<<"and string 4 is";

cout.write(names[3],len).put('\n');

getch();

}

Exchange positio of string