Sorting an Array of characters

I am suppose to write a program using a bubble sort to sort an array of characters. I keep getting the error "error C2440: 'initializing' : cannot convert from 'char' to 'const char *'. I can't figure out where Im going wrong. Thanks for any help.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cstring>
#include <conio.h>
using namespace std;

void pause();
void BubbleSort( const char *array[], int size);

int main()
{
	int i = 0;
	const int arraySize = 20;	
	const char *characters[ arraySize ] = {'b','z','w','q','d','t','c','u','f','s','o','x','a','e','v','z','l','r','I','g'};
	
	//Original print out of listing of characters	 
	cout << "The original array of characters:\n\n";	 
	for ( i = 0; i < arraySize; ++i )         
		cout << characters[ i ] << "\n" ;	 
	pause();

	//Sort the array
	BubbleSort (characters, arraySize);

	//Print out of characters in alphabetical order
	cout << "The alphabetical listing of characters:\n\n";         
	for ( i = 0; i < arraySize; ++i )         
		cout << characters[ i ] << "\n" ;          
	pause ();	 
	return 0;
}//main

void BubbleSort( const char *array[], int size)
{
	    int result;    
		//Performs a run through number of strings	
		for ( int pass = 0; pass < size - 1 ; ++pass )
		{
			//Runs through each string for compare		
			for ( int j = 0; j < size - 1 - pass; ++j )
			{		
				//Perform string compare and return value store as result                
				result = strcmp (array[j], array[j+1]);		
				//If value is less than 0 then perform swap function to rearrange                
				if (result > 0)		   
					swap ( array[j] , array[j+1] );		
			}//for		 	
		}//for
}//BubbleSort

void pause ()
{    
	cout << "\nPress any key to continue...";    
	getch();    
	cout << "\r";    
	cout << "                            ";    
	cout << "\r";
}//pause 
Wrong. You are creating an array of char pointers here where each element in the table would need to be a pointer to a separate array.

const char *characters[ arraySize ] = {'b','z','w','q','d','t','c','u','f','s','o','x','a','e','v','z','l','r','I','g'};

Right. This is a single array of characters where each element is one character.

char characters[arraySize] = {'b','z','w','q','d','t','c','u','f','s','o','x','a','e','v','z','l','r','I','g'};

Moreover, change the bubbleSort declaration to match. Get rid of the '*' in the declaration. There may be other problems but that one was significant.
Last edited on
Ok. I was confused about the whole pointer stuff. I took that part out because thats not what I was trying to do. I fixed all those parts throughout the code but now I'm still getting the same error even though I'm no longer using any pointers 'const char *' What does this error mean exactly? The error always points to line 42 where the result=...is. I dont get it. And thank you for your help.

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
45
46
47
48
#include <iostream>
#include <cstring>
#include <conio.h>
using namespace std;

void pause();
void BubbleSort(char Array[], int size);

int main()
{
	int i = 0;
	const int arraySize = 20;	
	char characters[arraySize] = {'b','z','w','q','d','t','c','u','f','s','o','x','a','e','v','z','l','r','I','g'};
	
	//Original print out of listing of characters	 
	cout << "The original array of characters:\n\n";	 
	for ( i = 0; i < arraySize; ++i )         
		cout << characters[ i ] << "\n" ;	 
	pause();

	//Sort the array
	BubbleSort (characters, arraySize);

	//Print out of characters in alphabetical order
	cout << "The alphabetical listing of characters:\n\n";         
	for ( i = 0; i < arraySize; ++i )         
		cout << characters[ i ] << "\n" ;          
	pause ();	 
	return 0;
}//main

void BubbleSort(char Array[], int size)
{
	    int result;    
		//Performs a run through number of strings	
		for ( int pass = 0; pass < size - 1 ; ++pass )
		{
			//Runs through each string for compare		
			for ( int j = 0; j < size - 1 - pass; ++j )
			{		
				//Perform string compare and return value store as result                
				result = strcmp (Array[j], Array[j+1]);		
				//If value is less than 0 then perform swap function to rearrange                
				if (result > 0)		   
					swap ( Array[j] , Array[j+1] );		
			}//for		 	
		}//for
}//BubbleSort 
Array is an array of characters. Array[x] is a character (char).

strcmp() expects two strings, ie, two const char* to compare.

Do you want to compare strings or characters?
He's doing bubblesort so I would assume he wants to compare characters.
You can use
1
2
3
4
5
6
7
8
9
>
>=
<
<=
==
!=
&
||
!

if you want to compare characters.
I got it to work.....had to change it to a flagged bubble sort though......thanks for your guys help
Topic archived. No new replies allowed.