?
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Hi,
I have a "funny" question, which I think is pretty "healthy" to
examine... This program is being investigated:
- - - - - - -
#include <iostream>
using namespace std;
#define DAYS 7
int main()
{
void bsort(char **, int);
char *p_string[DAYS] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday",
"Friday", "Saturday"};
cout << "Before sorting the pointers:\n\n";
for(int j=0; j<DAYS; j++)
cout << p_string[j] << endl;
bsort(p_string, DAYS);
cout << "After sorting the pointers:\n\n";
for(int j=0; j<DAYS; j++)
cout << p_string[j] << endl;
return 0;
}
void bsort(char **ptr, int n)
{
void order(char*&, char*&);
int j,k;
for(j=0; j<n-1; j++)
for(k=j+1; k<n; k++)
order(ptr[j], ptr[k]);
}
void order(char*& pp1, char*& pp2)
{
if( strcmp(pp1, pp2) > 0)
{
char *tempptr = pp1;
pp1 = pp2;
pp2 = tempptr;
}
}
- - - - -
It runs fine (just copy/paste)...
There are 2 posibilites (at least) that works:
The above has prototype inside bsort:
1) void order(char*&, char*&);
.... and:
void order(char*& pp1, char*& pp2)
{
if( strcmp(pp1, pp2) > 0)
{
char *tempptr = pp1;
pp1 = pp2;
pp2 = tempptr;
}
}
- - - - - - - - - - - - - - - - - - - - - -
But.... I can also do (prototype instide bsort):
2) void order(char*, char*);
.... and:
void order(char* pp1, char* pp2)
{
if( strcmp(pp1, pp2) > 0)
{
char *tempptr = pp1;
pp1 = pp2;
pp2 = tempptr;
}
}
This also works...
- - - - - - - - - - -
The difference between 1 and 2 is that if I place a breakpoint inside
order(), my debugger shows that using method...
1) pp1 = @0xbffffa60, pp2 = @0xbffffa64
and using the other method:
2) pp1 = 0xde80 (sunday), pp2 = 0xde8c (monday)
- - - - - - - - - - -
So the difference between method 1 and 2 is??? I didn't quite understand
it, but obviously there are 2 different ways in which the program works.
I see that method 2 directly shows (sunday) or (monday) or whatever from
inside the debugger. Method 1 has a strange @ in front of the address
and it doesn't show (sunday/monday). What's that @-thing? I would assume
this &-means alias to/reference to????
There are probably also other methods this program could work.... If
anyone wants to tell, go on and I hope I can learn to understand it
Best regards / Med venlig hilsen
Martin Jørgensen
I have a "funny" question, which I think is pretty "healthy" to
examine... This program is being investigated:
- - - - - - -
#include <iostream>
using namespace std;
#define DAYS 7
int main()
{
void bsort(char **, int);
char *p_string[DAYS] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday",
"Friday", "Saturday"};
cout << "Before sorting the pointers:\n\n";
for(int j=0; j<DAYS; j++)
cout << p_string[j] << endl;
bsort(p_string, DAYS);
cout << "After sorting the pointers:\n\n";
for(int j=0; j<DAYS; j++)
cout << p_string[j] << endl;
return 0;
}
void bsort(char **ptr, int n)
{
void order(char*&, char*&);
int j,k;
for(j=0; j<n-1; j++)
for(k=j+1; k<n; k++)
order(ptr[j], ptr[k]);
}
void order(char*& pp1, char*& pp2)
{
if( strcmp(pp1, pp2) > 0)
{
char *tempptr = pp1;
pp1 = pp2;
pp2 = tempptr;
}
}
- - - - -
It runs fine (just copy/paste)...
There are 2 posibilites (at least) that works:
The above has prototype inside bsort:
1) void order(char*&, char*&);
.... and:
void order(char*& pp1, char*& pp2)
{
if( strcmp(pp1, pp2) > 0)
{
char *tempptr = pp1;
pp1 = pp2;
pp2 = tempptr;
}
}
- - - - - - - - - - - - - - - - - - - - - -
But.... I can also do (prototype instide bsort):
2) void order(char*, char*);
.... and:
void order(char* pp1, char* pp2)
{
if( strcmp(pp1, pp2) > 0)
{
char *tempptr = pp1;
pp1 = pp2;
pp2 = tempptr;
}
}
This also works...
- - - - - - - - - - -
The difference between 1 and 2 is that if I place a breakpoint inside
order(), my debugger shows that using method...
1) pp1 = @0xbffffa60, pp2 = @0xbffffa64
and using the other method:
2) pp1 = 0xde80 (sunday), pp2 = 0xde8c (monday)
- - - - - - - - - - -
So the difference between method 1 and 2 is??? I didn't quite understand
it, but obviously there are 2 different ways in which the program works.
I see that method 2 directly shows (sunday) or (monday) or whatever from
inside the debugger. Method 1 has a strange @ in front of the address
and it doesn't show (sunday/monday). What's that @-thing? I would assume
this &-means alias to/reference to????
There are probably also other methods this program could work.... If
anyone wants to tell, go on and I hope I can learn to understand it
Best regards / Med venlig hilsen
Martin Jørgensen