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 :?:

That's because hulp is just a pointer to a character. Or possibly a character in an array of characters. Not some kind of magical string object. Since you haven't created an array of characters for hulp to point to, when you copy to whatever hulp is pointing to, you're writing to write to random memory, causing the error.

The prior version simply copies pointers, not strings.

... Since you haven't created an array of characters for hulp to point to, when you copy to whatever hulp is pointing to, you're writing to write to random memory, causing the error.

Bingo, :D

char *hulp;
hulp = new char [100];

Am I correct that it is best to write

delete[] hulp;

when I don't need that pointer to an array of characters anymore :?:

Also, could you give me a clue as to wether I'm correct in saying this:

...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[]"

Thanks for the help Rashakil Fol :!:

>Am I correct that the way "char* &p" is written, that this is because the parameters of sort2 are array's of pointers
No, they're references to pointers to char. The reference syntax is required to allow the function to rebind p and q and have the changes reflected back in the calling function. You could also pass a pointer to pointer to char and have the same result, but the syntax is less convenient.

>could they have been written differently, for instance like this
Yes, but probably not the way you're thinking. Instead of passing two pointers to sort2 (which is just a glorified swap), you would pass the whole of argv and actually do a complete sort.

>when I don't need that pointer to an array of characters anymore
Yes.

Ok, thanks for the explanation Narue.

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.