Hello all,

This is some code that another poster put up a while ago.
Just for the heck of it, I tried to compile his solution, but i can't get the poiner syntax on the bubblesort function to work. I modified the call in main() to take a ref instead of *array[], in order to get it to actually even ATTEMPT to enter the function. Otherwise I got a plethora of compiler complaints. I still, do but now they are confined to the function definition in this present form. I get a segmentation violation when strcmp() tries to read the arrays in.

You might ask why I don't use ref syntax instead? Thats because the OP had it this way, and I would like to better understand how to structure code that uses pointers.

Thanks in advance!

#include<iostream>
using namespace std;
#include<cstring>
using std::strncmp;
void BubbleSort(  const char *array[] , int size );


int main()
{
const int ArraySize = 10;
const char * Array[] = { "Toronto", "Montreal", "Alberta", "Quebec", "NewYork", "Calgary", "Edmonton", "NewJersey", "Ontario", "California" };


cout << "The unsorted arangement of the town names is: \n"<<endl;
for ( int i = 0; i < ArraySize; i++ )
{
cout <<" " << Array[i];
cout << endl << "\n";
}

BubbleSort( &Array[ArraySize] ,  ArraySize);
cout << "The sorted arangement of the town names is: \n"<<endl;


for ( int j = 0; j < ArraySize; j++ )
{
cout <<" " << Array[j];
cout << endl << "\n";
}



return 0;

}
void BubbleSort(  const char *array[] , int size )
{

    int result;

    for ( int next = 0; next < size - 1 ; ++next )
    {
        for ( int j = 0; j < size - 1 - next; ++j )
        {
            result = (strcmp (( array[j]),(  array[j+1])));

            if (result > 0)
                swap ( array[j] , array[j+1] );
        }
    }
}

See revised BubbleSort() and how Array is passed to it.

#include<iostream>
using namespace std;
#include<cstring>
void BubbleSort(  const char *array[] , int size );


int main()
{
const int ArraySize = 10;
const char * Array[] = { "Toronto", "Montreal", "Alberta", "Quebec", 
	"NewYork", "Calgary", "Edmonton", "NewJersey", "Ontario", "California" };


cout << "The unsorted arangement of the town names is: \n"<<endl;
for ( int i = 0; i < ArraySize; i++ )
{
cout <<" " << Array[i] << "\n";
}

BubbleSort( Array ,  ArraySize);
cout << "The sorted arangement of the town names is: \n"<<endl;


for ( int j = 0; j < ArraySize; j++ )
{
cout <<" " << Array[j] << "\n";
}



return 0;

}

void BubbleSort(  const char *array[] , int size )
{
    for ( int next = 0; next < size - 1 ; ++next )
    {
        for ( int j = next+1; j < size ; ++j )
        {
            if( strcmp ( array[next],array[j] ) > 0)
                swap ( array[next] , array[j] );
        }
    }
}

>BubbleSort( &Array[ArraySize] , ArraySize);
If the variable is declared the same as the parameter, you need only pass the variable name. The only time you need to play around with addresses or indirection in a function call is when you have a complex type and need a subset of that type. For example, if Array were an array of arrays of strings (let's say a list of countries where each country has a list of ten cities):

const char *Array[][10];

Then you wouldn't be able to call the function using just the name, you would need to index one of the countries first:

BubbleSort ( Array[x], n_cities );

However, since the parameter and the variable declarations match:

void BubbleSort(  const char *array[] , int size );
const char * Array[] = { "Toronto", "Montreal",...

All you need is the name:

BubbleSort ( Array, ArraySize );

Ne? :)

Wow ! that was simple! I
So, array pointers are passed by name only because the type declaration was in the prototype?
At one point the compiler did thinkI was trying to do a pointer to a pointer with *Array in the arg.

As a rule of thumb can I say that the prototype and the definition args must match, but the call can and will differ depending on how the arg is used in main? For instance , IF the code was set up for a pointer to a pointer, then *Array would be correct in the call?

>As a rule of thumb can I say that the prototype and the definition args
>must match, but the call can and will differ depending on how the arg is used in main?
Yes. In fact, that's exactly the rule of thumb that saves people from having to worry about how to pass multi-dimensional arrays. :)

>IF the code was set up for a pointer to a pointer, then *Array would be correct in the call?
*Array gives you the first string, "Toronto" in this case. It would be a pointer to const char, so no. This is a tricky area because the code is already set up for a pointer to a pointer. When you pass an array to a function, the first dimension is converted to a pointer. So these two declarations are identical:

void foo ( int array[] );
void bar ( int *array );

The declaration for BubbleSort could be changed the same way:

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

So Array is still correct when working with pointers to pointers in this case.

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.