C++ Primer ex 7.5

A

arnuld

as usual , advices for improvement:


/* C++ Primer - 4/e
*
* exercise 7.5
* STATEMENT:
* write a funtion that take an int and a pointer to an int and
return the larger of the values. What type should you use for the
pointer ?
*/


#include <iostream>
#include <cassert>

/* we will use pointer to a const because we are interested in
only reading the values */
int return_bigger( int i, const int* ip) {
if( i > *ip )
{
return 1;
}
else if( i < *ip )
{
return -1;
}
else
{
return 0; /* if both vales are equal.t his will return Zero */
}
}


int main()
{
std::cout << "enter an initeger: ";
int i;
std::cin >> i;

std::cout << "enter an initeger: ";
int j;
std::cin >> j;

const int* pj = &j;

std::cout << "is "
<< i
<< " bigger than "
<< j
<< ":\t"
<< (return_bigger( i, pj ))
<< std::endl;

return 0;


}

====== OUTPUT =========
[arnuld@arch cpp] $ g++ -ansi -pedantic -Wall -Wextra ex_07-05.cpp
[arnuld@arch cpp] $ ./a.out
enter an initeger: 3
enter an initeger: 2
is 3 bigger than 2: 1
[arnuld@arch cpp] $ ./a.out
enter an initeger: -9
enter an initeger: -10
is -9 bigger than -10: 1
[arnuld@arch cpp] $ ./a.out
enter an initeger: 3
enter an initeger: 3
is 3 bigger than 3: 0
[arnuld@arch cpp] $
 
F

Frank Birbacher

Hi!
/* we will use pointer to a const because we are interested in
only reading the values */
int return_bigger( int i, const int* ip) {

I'd place more "const":
int return_bigger(const int i, const int* const ip) {


BTW, I understand the "STATEMENT" and the function name "return_bigger"
to actually return the bigger value:

int return_bigger(const int i, const int* const ip) {
return i>*ip ? i : *ip ;
}

Frank
 
A

Alf P. Steinbach

* arnuld:
as usual , advices for improvement:


/* C++ Primer - 4/e
*
* exercise 7.5
* STATEMENT:
* write a funtion that take an int and a pointer to an int and
return the larger of the values. What type should you use for the
pointer ?
*/


#include <iostream>
#include <cassert>

/* we will use pointer to a const because we are interested in
only reading the values */
int return_bigger( int i, const int* ip) {
if( i > *ip )
{
return 1;
}
else if( i < *ip )
{
return -1;
}
else
{
return 0; /* if both vales are equal.t his will return Zero */
}
}


int main()
{
std::cout << "enter an initeger: ";
int i;
std::cin >> i;

std::cout << "enter an initeger: ";
int j;
std::cin >> j;

const int* pj = &j;

std::cout << "is "
<< i
<< " bigger than "
<< j
<< ":\t"
<< (return_bigger( i, pj ))
<< std::endl;

return 0;


}

====== OUTPUT =========
[arnuld@arch cpp] $ g++ -ansi -pedantic -Wall -Wextra ex_07-05.cpp
[arnuld@arch cpp] $ ./a.out
enter an initeger: 3
enter an initeger: 2
is 3 bigger than 2: 1
[arnuld@arch cpp] $ ./a.out
enter an initeger: -9
enter an initeger: -10
is -9 bigger than -10: 1
[arnuld@arch cpp] $ ./a.out
enter an initeger: 3
enter an initeger: 3
is 3 bigger than 3: 0
[arnuld@arch cpp] $

If you change your interpretation of the problem statement a little,
then you can implement the function using std::max().
 
B

Barry

Frank said:
Hi!


I'd place more "const":
int return_bigger(const int i, const int* const ip) {
no need here I think, as the pointer variable `ip' is a local variable
in the function `return_bigger' scope, change what `ip' points to has
no effect on the outside of this function, so const pointer or not is
dependent on the function `return_bigger' itself.
 
A

arnuld

BTW, I understand the "STATEMENT" and the function name "return_bigger"
to actually return the bigger value:

int return_bigger(const int i, const int* const ip) {
return i>*ip ? i : *ip ;
}

Frank

WOW Frank.... so compact, so smart way. i will use it :)
 
A

arnuld

change your interpretation of the problem statement a little, then you
can implement the function using std::max().

i knew someone will mention it. i love to use standard library algorithms
and Templates and i do not possess any knowledge of them yet. i am at
chapter 7 and whn i try to read standard library's Templates and
Algorithms from Stroustrup, my head hurts :(

may be i need more experience with C++ and i am determined to have it :)


(BTW, if after 1 or 2 years, i will get to teach someone C++ theni surely
wil teach im 1st about std::max, std::eek:stream 1st rathe rthan do-while or
if, just my opinion)
 
F

Frank Birbacher

Hi!
no need here I think, as the pointer variable `ip' is a local variable
in the function `return_bigger' scope, change what `ip' points to has
no effect on the outside of this function, so const pointer or not is
dependent on the function `return_bigger' itself.

Correct. It's the same with "i". Well, "void foo(int)" and "void
foo(const int)" are even the same signature. It is referred to as "top
level const". And "top level const" does not change the signature. All
it does is to help you not to shoot yourself in the foot. If you do not
intend to deliberately change the parameter value (that is "i" or "ip",
not "*ip") you should make it "const".

Frank
 
F

Frank Birbacher

Hi!
then you can implement the function using std::max().

Of course. But I think that is not the point of the excercise. Otherwise
the STATEMENT could be "Find a std function which returns the bigger of
two values." (yes, std::max is not bigger_value, but that's not my
point). Same with the other excercises.

Frank
 
B

Barry

Frank said:
Hi!


Correct. It's the same with "i". Well, "void foo(int)" and "void
foo(const int)" are even the same signature. It is referred to as "top
level const". And "top level const" does not change the signature. All
it does is to help you not to shoot yourself in the foot. If you do not
intend to deliberately change the parameter value (that is "i" or "ip",

IMHO
yep, it's trivial, the formal param in the function works as two actor :
receive the passed-in value, also as an ordinary variable.

void f(int i) {
std::cout << i << std::endl;
// though we don't change i's value , we seldom make it const
}

IMHO
it's more sophisticated to make `i' const if the function is long enough
to remind the author that this i is not alterable within the function body
 
B

Barry

Frank said:
Hi!


Correct. It's the same with "i". Well, "void foo(int)" and "void
foo(const int)" are even the same signature. It is referred to as "top

Same signature?
I have no idea here

I don't know what exactly function signature is defined.

You meant they are the same maybe it's because you can't have overload
them at the same time.
But their `typeinfo' are not the same
 
F

Frank Birbacher

Hi!
Same signature?
I have no idea here

I don't know what exactly function signature is defined.

You give a function signature when you declare a function:

void foo(int);

The signature includes return type and the types of the parameters. That
is "void (int)".
You meant they are the same maybe it's because you can't have overload
them at the same time.

Correct. You cannot have them be different (overloaded).

void foo(int i) {}
void foo(const int i) {} //ERROR, "foo" already defined
But their `typeinfo' are not the same

Yes, they are:

#include <iostream>
#include <ostream>
#include <typeinfo>

int main()
{
const bool what = typeid(void (*) (int)) == typeid(void (*) (const int));
std::cout << what << '\n';
}

this prints "1" on my system, meaning that the typeids are the same.

Frank
 
B

Barry

Frank said:
Hi!


You give a function signature when you declare a function:

void foo(int);

The signature includes return type and the types of the parameters. That
is "void (int)".


Correct. You cannot have them be different (overloaded).

void foo(int i) {}
void foo(const int i) {} //ERROR, "foo" already defined


Yes, they are:

#include <iostream>
#include <ostream>
#include <typeinfo>

int main()
{
const bool what = typeid(void (*) (int)) == typeid(void (*) (const int));
std::cout << what << '\n';
}

this prints "1" on my system, meaning that the typeids are the same.


case 1:

void f1(int) {}
void f2(int const) {}

int main()
{
assert(typeid(void (*) (int)) == typeid(void (*) (const int)));
assert(typeid(f1) == typeid(f2));
}

OK

///////////////////////////////////

case2:
void f1(int* const) {}
void f2(int*) {}

int main()
{
assert(typeid(void (*)(int* const)) != typeid(void (*)(int*)));
}

OK

which means int* is not like int

case 3:
void f(int* const) {}
void f(int*) {}

int main()
{
}

Compile error: f violates the ODR

/////////////////////////////////////

why pointer type is treated special?
 
F

Frank Birbacher

Hi!
case2:
void f1(int* const) {}
void f2(int*) {}

int main()
{
assert(typeid(void (*)(int* const)) != typeid(void (*)(int*)));
}

OK

which means int* is not like int

On my system using gcc 4.1.2:

#include <iostream>
#include <ostream>
#include <typeinfo>

int main()
{
const bool what = typeid(void (*) (int*)) == typeid(void (*) (int*const));
std::cout << what << '\n';
}

prints "1", meaning they are the same.

Which compiler are you using?


Frank
 
B

Barry

Frank said:
Hi!


On my system using gcc 4.1.2:

#include <iostream>
#include <ostream>
#include <typeinfo>

int main()
{
const bool what = typeid(void (*) (int*)) == typeid(void (*) (int*const));
std::cout << what << '\n';
}

prints "1", meaning they are the same.

Which compiler are you using?

icl 9.1 and msvc8.0 and 6.0
 
F

Frank Birbacher

Hi!
icl 9.1 and msvc8.0 and 6.0

I get "0" using msvc8 express. I get "1" using mingw gcc 3.4.2.

Strangely enough following works in msvc8:

template<int n, typename T>
struct generate_error
{
char array[n];
};

template<typename T>
struct test
{
generate_error<-1, T> e;
};

template<>
struct test<void (*) (int* const)>
{
//OK
};

int main()
{
//test<void> t1; //ERROR
test<void (*)(int*)> t2;
}

There is not compiler error. Thus with the typeids msvc is plainly
wrong. But in templates the top level const is ignored. Just like with
function declarations (overloads not allowed).

Frank
 
A

Alf P. Steinbach

* Frank Birbacher:
Hi!


Of course. But I think that is not the point of the excercise. Otherwise
the STATEMENT could be "Find a std function which returns the bigger of
two values." (yes, std::max is not bigger_value, but that's not my
point). Same with the other excercises.

Presumably the point of the exercise was to do a little pointer argument
passing and dereferencing.
 
B

Barry

Frank said:
Hi!
icl 9.1 and msvc8.0 and 6.0

I get "0" using msvc8 express. I get "1" using mingw gcc 3.4.2.

Strangely enough following works in msvc8:

template<int n, typename T>
struct generate_error
{
char array[n];
};

template<typename T>
struct test
{
generate_error<-1, T> e;
};

template<>
struct test<void (*) (int* const)>
{
//OK
};

int main()
{
//test<void> t1; //ERROR
test<void (*)(int*)> t2;
}

There is not compiler error. Thus with the typeids msvc is plainly
wrong. But in templates the top level const is ignored. Just like with
function declarations (overloads not allowed).

Well, did you test this on gcc?

It should work since gcc already
assert( typeid( void(*)(int*) ) == typeid( void (*)(int* const) ) )
 
J

James Kanze

i knew someone will mention it. i love to use standard library algorithms
and Templates and i do not possess any knowledge of them yet. i am at
chapter 7 and whn i try to read standard library's Templates and
Algorithms from Stroustrup, my head hurts :(
may be i need more experience with C++ and i am determined to have it :)
(BTW, if after 1 or 2 years, i will get to teach someone C++ theni surely
wil teach im 1st about std::max, std::eek:stream 1st rathe rthan do-while or
if, just my opinion)

I don't know about std::max, but I'd certainly teach them about
std::vector before abording T[]. (Well, maybe not all of the
subtilities of std::vector right off the bat.)

More generally, regardless of the language I was teaching, I'd
very quickly teach them to use the documentation to try to find
whether what they were trying to do already existed. (But some
of the later exercises would be: do this without using function
that.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C++ Primer ex 7.6 17
C++ Primer ex 3.24 (bitset class) 5
C++ Primer ex 9.27 4
Lexical Analysis on C++ 1
C++ Primer exercise 3.13 17
C++ Primer section 1.6 example 1
C++ Primer ex 3.14 8
Character operations in C++ 2

Members online

No members online now.

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top