Hello ladies and gents,
Came across a program example in wich you are able to sort program-parameters in alphabetical order. The code is this:
#include <iostream>
#include <string>
using namespace std;
void sort2(char* &p, char* &q)
{
if(strcmp(p, q)> 0)
{
char *h;
h = p; p = q; q = h;
}
}
int main(int argc, char *argv[])
{
int i;
cout<< "argc = "<< argc <<endl;
if(argc == 4)
{
sort2(argv[1], argv[2]);
sort2(argv[2], argv[3]);
sort2(argv[1], argv[3]);
for (i = 1; i < argc; i++)
cout<<"argv["<< i <<"] = "<< argv[i] <<endl;
}
else
cout<<"Give three program-parameters!\n";
cout<<"Press any key to continue!\n";cin.get();
return 0;
}
My question about this code is this part:
void sort2(char* &p, char* &q)
Am I correct that the way "char* &p" is written, that this is because the parameters of sort2 are array's of pointers:?:
If so, could they have been written differently, for instance like this:
"char *p[]" :?:
Also, I changed the sorting from this:
char *h;
h = p; p = q; q = h;
into this:
char *hulp;
strcpy(hulp, p);
strcpy(p, q);
strcpy(q, hulp);
I just wanted to see wether with the use of strcpy I could make this work aswell, apparantly not :confused:
Though not getting error messages when debugging, I do have one warning in that local variable "hulp" isn't initialized.
When executing the program, I get a message saying that there was an error during the execution of the program :?: