468,496 Members | 1,801 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

swap char array.

NK
char *a="nnnnnnn fgjfjgjf";
char *b="ABC jjjjjjjjjhhhh";

to swap these two string......
this swap fn is working

template<class T>
void Swap( const T *&a,const T *&b)//no need to write const
{
T *temp;
temp=a;

a=b;

b=temp;
}

but to swap
char a[]="nhfhdhfhh fgjfjgjf";
char b[]="ABC jjjjjjjjjhhhh";

these char array its not.
why?
how a[] and b[] can be swapped?
give effficient soln.
don't use swap fn of string library(string1.swap(string2))

Thanx in advance
Dec 26 '07 #1
3 11416
NK wrote:
char *a="nnnnnnn fgjfjgjf";
char *b="ABC jjjjjjjjjhhhh";

to swap these two string......
this swap fn is working

template<class T>
void Swap( const T *&a,const T *&b)//no need to write const
{
T *temp;
temp=a;

a=b;

b=temp;
}

but to swap
char a[]="nhfhdhfhh fgjfjgjf";
char b[]="ABC jjjjjjjjjhhhh";

these char array its not.
why?
how a[] and b[] can be swapped?
give effficient soln.
don't use swap fn of string library(string1.swap(string2))

Here you go:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.2
Dec 26 '07 #2
On Dec 26, 11:33 pm, NK <nagendra...@gmail.comwrote:
char *a="nnnnnnn fgjfjgjf";
char *b="ABC jjjjjjjjjhhhh";

to swap these two string......
this swap fn is working

template<class T>
void Swap( const T *&a,const T *&b)//no need to write const
{
T *temp;
temp=a;

a=b;

b=temp;

}

but to swap

char a[]="nhfhdhfhh fgjfjgjf";
char b[]="ABC jjjjjjjjjhhhh";

these char array its not.
why?
how a[] and b[] can be swapped?
give effficient soln.
don't use swap fn of string library(string1.swap(string2))

Thanx in advance
swap function could also check if both the reference are pointing to
same variable or not...
Dec 27 '07 #3
On 26 Dec, 18:33, NK <nagendra...@gmail.comwrote:
char *a="nnnnnnn fgjfjgjf";
* char *b="ABC jjjjjjjjjhhhh";

to swap these two string......
*this swap fn is working

template<class T>
void Swap( const T *&a,const *T **&b)//no need to write const
{
* * *T *temp;
* * *temp=a;

* * *a=b;

* * *b=temp;

}

but to swap

char a[]="nhfhdhfhh fgjfjgjf";
* * * *char b[]="ABC jjjjjjjjjhhhh";

these char array its not.
why?
how a[] and b[] can be swapped?
give effficient soln.
don't use swap fn of string library(string1.swap(string2))

Thanx in advance
Obviously homework, but it's Christmas ...

You can't do this directly - arrays are not assignable.
You could do a character-by-character swap, but then
what if the arrays are of different lengths?

But all of this is academic, you can achieve what you
need like this:

char a[]="nhfhdhfhh fgjfjgjf";
char b[]="ABC jjjjjjjjjhhhh";

char* ap = a;
char* bp = b;

// use ap for a, bp for b

std::swap(ap, bp);

// ap now points to b[], bp to a[]
Dec 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

38 posts views Thread by JKop | last post: by
5 posts views Thread by dbuser | last post: by
12 posts views Thread by Eugen J. Sobchenko | last post: by
8 posts views Thread by meendar | last post: by
9 posts views Thread by ma740988 | last post: by
5 posts views Thread by Bo Berglund | last post: by
8 posts views Thread by silversnake | last post: by
11 posts views Thread by Dennis Jones | last post: by
4 posts views Thread by Robert LaMarca | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.