Michael Kochetkov wrote:
Michael Kochetkov wrote:
[discussion trimmed]
It is a pity.
think of all that bandwidth we saved ...
What seems to be the problem ?
The key words were about hexdigits [xxx] on the stack.
The OP was unspecific here but we should point it out that
But he was. hexdigits declaration and << operator call were in the same
scope, at least I read it like that. Such a situation is ill-formed in a
global scope.
CODE-A
const char hexdigits [17] = "0123456789ABCDEF" ;
void func2( int Printit )
{
cout << hexdigits [ Printit ] ;
}
is better than
CODE-B
void func2( int Printit )
{
const char hexdigits [17] = "0123456789ABCDEF" ;
cout << hexdigits [ Printit ] ;
}
Well, I have not expected to start a discussion indeed. I have just given a
useful hint about the code for the OP and those who given answers before.
May be I shall rely on hints less.
So, here goes some elaborations. const char hexdigits [17] and char
hexdigits [17] are quite different indeed and I was of Pete's opinion that a
smart compiler may omit copying. But as I have mentioned above I was not
sure and decided to check it with industry standard compilers and not only
them. To my surprise all of them used copying.
Franckly speaking, I was reluctant to peep into the Standard but I think the
following:
const char * cp = "0123456789ABCDEF" ;
and
const char hexdigits [17] = "0123456789ABCDEF" ;
are different things. And I believe, that later allows const_cast. If it is
true then a compiler must use copying in the second example.