Having a hard time here, Im not great with pointers and arrays yet, can anyone tell me how to properly pass a char array, i thought this was right but the compiler is complaining with:

I know the bubble sort is incomplete, i just wanted to quickly test the passing values when this error stopped me.

SORT.CPP: In function ‘int main()’:
SORT.CPP:28: error: cannot convert ‘char (*)[15]’ to ‘char*’ for argument ‘1’ to ‘void bub_sort(char*, int)’
#include <iostream>
#include <fstream>
using namespace std;

char test_ar[6][15] = { 'c','d','b','a','e'};
// swap two values 
void swap(char *x,char *y);
void bub_sort(char*, int);

void bub_sort(char* char_array, int length) { 
int i,k;

for (k=0;k<=length;k++) { 
for (i=0;i<=length;i++) { 
if (char_array[i+1] > char_array[i]) { 
			cout << "char_array[i+1] is bigger: " << char_array[i+1] <<	" than " << char_array[i];
				} // end char
	
		} 
	}
	
} 


int main() { 

bub_sort(test_ar,5);	
	
return 0;	
}

Thanks in advance.

Recommended Answers

All 4 Replies

line 6 is declared wrong. Try this: char test_ar[] = "cdbae";

And you have wrong for loop limits in bub_sort...

Thank you guys, I fixed it. I decided to use strings instead of char arrays and i changed the sort so that its using j-1 instead of j+1 (Segfaults are bad). Let me ask, is it more conventional coding to use strings in c++ instead of char arrays, or vice versa? Im still pretty wet behind the ears, but I prefer the strings.

>Let me ask, is it more conventional coding to use strings in c++ instead of char arrays, or vice versa?
It's more conventional to use strings unless you have a good reason not to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.