Swapping pointers

I am having trouble with swapping pointers, can anyone teach me ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# include "header.h" 

int main (void) 
{
	int max,count = 0 ; 
	char **name ;
	char temp[80] ;
	cout << "Please key the maximum number string : " ;
	cin >> max ;
	cin.ignore(80,'\n') ;
	name = new char*[max] ;
	for(int i = 0 ; i < max ; i++)
	{
		
		cout <<"Please enter name for " 
			 << i + 1
			 <<" student : " ;
		cin.getline(temp,81,'\n') ;
		if(temp[0] == 'e' && temp[1] == 'n' && temp[2] == 'd')
		{
			break ;
		}
		cout <<endl ;
		name[i] = new char[strlen(temp)+1] ;
		count ++ ;
	}

	for (int j = 0 ; j <max ; j++)
	{
		for (int k = 0 ; k<max ; k++)
		{
			if(strcmp(name[k],name[k+1])>0)
			{
				strcpy(temp,name[k]) ;
				strcpy(name[k],name[k+1]) ;
				strcpy(name[k+1],temp) ;
			}
		}
	}

	system("pause") ;
	return 0 ;

}
Last edited on
What do you want to swap here?
umm I want to swap array of characters that has been key by the user as records

It looks as if you are trying to swap name[k] with name[k+1], but why would you want to do that? You never define them...

Anyway, if you want to swap char a[80] with char b[80]
1
2
3
4
char a[80], b[80], c[80];
strcpy(c, a);
strcpy(a, b);
strcoy(b, c);

or use string class to make things more simple
1
2
string a, b;
swap(a, b);

or if you want to swap pointers
1
2
3
4
char* a, *b, *c;
c = a;
a = b;
b = c;
um what do you mean by i never define them... I am trying to reorder the records as alphabatical order.
I mean that you never move users input into them
I believe, there should be strcpy(name[i], temp); after line 24.
Okay thanks. This solve my problem.
Topic archived. No new replies allowed.