By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,616 Members | 2,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,616 IT Pros & Developers. It's quick & easy.

C++ Primer ex 7.5

P: n/a
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] $

--
http://arnuld.blogspot.com

Aug 10 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Hi!

arnuld schrieb:
/* 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
Aug 10 '07 #2

P: n/a
Frank Birbacher wrote:
Hi!

arnuld schrieb:
>/* 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) {
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.
>
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
Aug 10 '07 #3

P: n/a
On Fri, 10 Aug 2007 13:21:35 +0200, Frank Birbacher wrote:
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 :)
--
http://arnuld.blogspot.com

Aug 10 '07 #4

P: n/a
On Fri, 10 Aug 2007 13:31:38 +0200, Alf P. Steinbach wrote: If you
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::ostream 1st rathe rthan do-while or
if, just my opinion)

--
http://arnuld.blogspot.com

Aug 10 '07 #5

P: n/a
Hi!

Barry schrieb:
Frank Birbacher wrote:
>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.
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
Aug 10 '07 #6

P: n/a
Hi!

Alf P. Steinbach schrieb:
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
Aug 10 '07 #7

P: n/a
Frank Birbacher wrote:
Hi!

Barry schrieb:
>Frank Birbacher wrote:
>>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.

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

not "*ip") you should make it "const".
Aug 10 '07 #8

P: n/a
Frank Birbacher wrote:
Hi!

Barry schrieb:
>Frank Birbacher wrote:
>>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.

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

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".
Aug 10 '07 #9

P: n/a
Hi!

Barry schrieb:
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
Aug 10 '07 #10

P: n/a
Frank Birbacher wrote:
Hi!

Barry schrieb:
>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.

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?

Aug 10 '07 #11

P: n/a
Hi!

Barry schrieb:
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
Aug 10 '07 #12

P: n/a
Frank Birbacher wrote:
Hi!

Barry schrieb:
>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?
icl 9.1 and msvc8.0 and 6.0

Aug 10 '07 #13

P: n/a
Hi!

Barry schrieb:
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, Te;
};

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

int main()
{
//test<voidt1; //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
Aug 10 '07 #14

P: n/a
Frank Birbacher wrote:
Hi!

Barry schrieb:
>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, Te;
};

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

int main()
{
//test<voidt1; //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) ) )
Aug 10 '07 #15

P: n/a
Hi!

Barry schrieb:
Well, did you test this on gcc?
Yes, on both mingw 3.4.2 and linux gcc 4.1.2. Both work.

Frank
Aug 10 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.