1
0
Fork 0

Ex2.1: feedback: mixed up pointers and references

This commit is contained in:
Eric Teunis de Boone 2020-06-30 10:29:44 +02:00
parent 982866a7e6
commit c49a9f3e5b
2 changed files with 27 additions and 27 deletions

View file

@ -83,8 +83,8 @@ void orderptr( int* a, int* b ) {
return; return;
} }
int* tmp = a; int tmp = *a;
a = b; *a = *b;
b = tmp; *b = tmp;
} }
#endif #endif

View file

@ -15,9 +15,9 @@
void sort( const char** A, const int size ); void sort( const char** A, const int size );
#if !USE_PTR #if !USE_PTR
void order( const char* a, const char* b ); void order( const char* &a, const char* &b );
#else #else
void orderptr( const char* &a, const char* &b ); void orderptr( const char* *a, const char* *b );
#endif #endif
@ -78,34 +78,14 @@ void sort( const char** A, const int size ) {
#if !USE_PTR #if !USE_PTR
order( A[ i ], A[ j ] ); order( A[ i ], A[ j ] );
#else #else
orderptr( A[ i ], A[ j ] ); orderptr( &A[ i ], &A[ j ] );
#endif #endif
} }
} }
} }
#if !USE_PTR #if !USE_PTR
void order( const char* a, const char* b ) { void order( const char* &a, const char* &b ) {
#if VERBOSE
std::cout << "Order: " << a << std::endl;
#endif
// b is greater or equal to a, do nothing
if ( strcmp(a, b) > 0) {
#if VERBOSE
std::cout << "Ordering OK" << std::endl;
#endif
return;
}
#if VERBOSE
std::cout << "Ordering ToDo" << std::endl;
#endif
const char* tmp = a;
a = b;
b = tmp;
}
#else
void orderptr( const char* &a, const char* &b ) {
#if VERBOSE #if VERBOSE
std::cout << "Order: " << a << ", " << b << std::endl; std::cout << "Order: " << a << ", " << b << std::endl;
#endif #endif
@ -123,4 +103,24 @@ void orderptr( const char* &a, const char* &b ) {
a = b; a = b;
b = tmp; b = tmp;
} }
#else
void orderptr( const char* *a, const char* *b ) {
#if VERBOSE
std::cout << "Order: " << a << std::endl;
#endif
// b is greater or equal to a, do nothing
if ( strcmp(*a, *b) > 0) {
#if VERBOSE
std::cout << "Ordering OK" << std::endl;
#endif
return;
}
#if VERBOSE
std::cout << "Ordering ToDo" << std::endl;
#endif
const char* tmp = *a;
*a = *b;
*b = tmp;
}
#endif #endif