473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C++ Primer ex 7.5

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
15 1389
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Sandman | last post by:
Could anyone give me a tip about a good primer on object oriented php programming - why I should use it, the benefits, the drawbacks, the bugs, the glory? And, should I upgrade to php5 before...
1
by: Charles L | last post by:
Does anyone know where I can find errata for Stan Lippman's 'C++ Primer 2nd Edition'? Charles Leng
1
by: hugo | last post by:
what is L&L ,people or book?
5
by: hajime | last post by:
I purchased this book: C++ Primer, 4th Edition ISBN: 0201721481 in Australia. The right side of the last three lines on page 231 are not legible. A stain shows a piece of paper was on that part...
7
by: Lycan. Mao.. | last post by:
Hello, I am a newbie in C++ and I'm in trouble in choosing books, I hope some one who can give me some tips. I'm already know C and a little about Scheme, C#, Python, Lua and so on, and now I want...
2
by: W. Watson | last post by:
Is there a primer out there on these two items? I have the Python tutorial, but would like either a Tkinter tutorial/primer to supplement it, or a primer/tutorial that addresses both. Maybe there's...
20
by: arnuld | last post by:
I get an error, can't find what is the problem: /* C++ Primer - 4/e * * Chapter 8, exercise 8.3 * STATEMENT * write a function that takes and returns an istream&. the function should read...
2
by: xianwei | last post by:
First, typedef struct pair { Node *parent; Node *child; } Pair; static Pair SeekItem(cosnt Item *pI, const Tree *pTree) { Pair look;
1
by: Kveldulv | last post by:
Hi all, here is the code: http://pastebin.com/m6e74d36b I'm stuck at highfink constructor, last line before #endif. As parameters, I have reference to one parent class and int member of another...
0
by: cincerite | last post by:
Hello , guys , I'm reading C++ Primer 3rd edition recently.I tried to download the errata of it from Stan Lippman's Home Page:http:// staff.develop.com/slip/ ,but it says:We're Sorry, we could not...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.