about constants...

W

Who_Knows_Geek

#include <stdlib.h>

int main()
{
const char *strLeft = (const char*) malloc(10); // Left-operand
const char *strRight = (const char*) malloc(10); // Right-operand

strLeft = "123456789"; // Nine characters + the null-terminator.
strRight = "123456789"; // Nine characters + the null-terminator.

return 0;
}

This code is valid (no error) in VC++. WHY?
Isn't the memory refered by variables strLeft/strRight should've been
considered 'initialized' after the declaration?

'Just curious'
 
A

Alf P. Steinbach

* Who_Knows_Geek:
#include <stdlib.h>

int main()
{
const char *strLeft = (const char*) malloc(10); // Left-operand
const char *strRight = (const char*) malloc(10); // Right-operand

strLeft = "123456789"; // Nine characters + the null-terminator.
strRight = "123456789"; // Nine characters + the null-terminator.

return 0;
}

This code is valid (no error) in VC++. WHY?
Isn't the memory refered by variables strLeft/strRight should've been
considered 'initialized' after the declaration?

'Just curious'

Always write 'const' after what it refers to, like

char const * strLeft = ...

which can be read from right to left as "strLeft is a (non-const) pointer
to const char", whereas

char * const strLeft = ...

"strLeft is a const pointer to (non-const) char", and so on.

Also, buy a book and find the FAQ. ;-)
 
D

David Fisher

Who_Knows_Geek said:
#include <stdlib.h>

int main()
{
const char *strLeft = (const char*) malloc(10); // Left-operand
const char *strRight = (const char*) malloc(10); // Right-operand

strLeft = "123456789"; // Nine characters + the null-terminator.
strRight = "123456789"; // Nine characters + the null-terminator.

return 0;
}

This code is valid (no error) in VC++. WHY?
Isn't the memory refered by variables strLeft/strRight should've been
considered 'initialized' after the declaration?

Try making it char *const strLeft ...

See http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.5
for explanation.

David Fisher
Sydney, Australia
 
S

Sreenivas

From: Who_Knows_Geek ([email protected])
Subject: about constants...
Newsgroups: comp.lang.c++
Date: 2004-10-20 19:39:13 PST


#include <stdlib.h>

int main()
{
const char *strLeft = (const char*) malloc(10); // Left-operand
const char *strRight = (const char*) malloc(10); // Right-operand

strLeft = "123456789"; // Nine characters + the null-terminator.
strRight = "123456789"; // Nine characters + the null-terminator.

return 0;
}

This code is valid (no error) in VC++. WHY?
Isn't the memory refered by variables strLeft/strRight should've been
considered 'initialized' after the declaration?

'Just curious'

This piece of code is valid for any compiler.
u r declaring, the _memory_ pointed by strLeft and strRight are
constants, they themselves r not constants.

and u can make them point to any memory which is constant and the
string literal "123456789" is a constant string literal.

but if u do a strcpy(strLeft, strRight) your compiler will rise an
error.
saying that "trying to modify constant object" I hope that answers ur
query.

-Sreenivas.
 
R

Randy Yates

* Who_Knows_Geek:

Always write 'const' after what it refers to, like

char const * strLeft = ...

which can be read from right to left as "strLeft is a (non-const) pointer
to const char", whereas

So what is "const char *strLeft"? Is it equivalent to "char *strLeft" (i.e.,
is the constant keyword ignored)?
char * const strLeft = ...

"strLeft is a const pointer to (non-const) char", and so on.

I would alternately suggest using the clockwise spiral rule, a copy of
which is here:

http://home.earthlink.net/~yatescr/cwrule.htm

Note that I would make an import modification to this rule and call it
the "upward clockwise spiral rule," i.e., when beginning at the identifier,
go up first, then clockwise.
--
% Randy Yates % "I met someone who looks alot like you,
%% Fuquay-Varina, NC % she does the things you do,
%%% 919-577-9882 % but she is an IBM."
%%%% <[email protected]> % 'Yours Truly, 2095', *Time*, ELO
http://home.earthlink.net/~yatescr
 
K

Karl Heinz Buchegger

Who_Knows_Geek said:
#include <stdlib.h>

int main()
{
const char *strLeft = (const char*) malloc(10); // Left-operand
const char *strRight = (const char*) malloc(10); // Right-operand

strLeft = "123456789"; // Nine characters + the null-terminator.
strRight = "123456789"; // Nine characters + the null-terminator.

return 0;
}

This code is valid (no error) in VC++. WHY?
Isn't the memory refered by variables strLeft/strRight should've been
considered 'initialized' after the declaration?

What you did is this:

const char *strLeft

strLeft
+---------+
| |
+---------+

const char *strLeft = (const char*) malloc(10)


strLeft
+---------+ +---+---+---+---+---+---+---+---+---+---+
| o----------------------->| | | | | | | | | | |
+---------+ +---+---+---+---+---+---+---+---+---+---+

....
strLeft = "123456789"; // Nine characters + the null-terminator.

strLeft
+---------+ +---+---+---+---+---+---+---+---+---+---+
| o---------+ | | | | | | | | | | |
+---------+ | +---+---+---+---+---+---+---+---+---+---+
|
| +---+---+---+---+---+---+---+---+---+---+
+---->| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | \0|
+---+---+---+---+---+---+---+---+---+---+

I gues you expected with the assignment that the characters were copied
into the array you allocated previously. That is not going to happen. Arrays
are never copied this way. What is happening instead is that the pointer
strLeft is reaseated to point to something different. You declared the pointer
to be:
const char *strLeft;

Start from the variable

strLeft strLeft is
*strLeft strLeft is a pointer
char *strLeft strLeft is a pointer to characters
const char *strLeft strLeft is a pointer to characters which are const

In other words: The characters are constant, but not the pointer value itself.
The pointer can be reseated to whatever you like. The const would only get into
your way when you try to modify the characters themself, which you didn't.
 
K

Karl Heinz Buchegger

Randy said:
So what is "const char *strLeft"? Is it equivalent to "char *strLeft" (i.e.,
is the constant keyword ignored)?

No. const always applies to the thing on its left. With the exception if const
is the left most thing, then it applies to the thing on its right.

So
const char * something
and
char const * something

are exactly the same thing.
 
R

Randy Yates

Karl Heinz Buchegger said:
[...]
Randy said:
So what is "const char *strLeft"? Is it equivalent to "char *strLeft" (i.e.,
is the constant keyword ignored)?

No. const always applies to the thing on its left. With the exception if const
is the left most thing, then it applies to the thing on its right.

So
const char * something
and
char const * something

are exactly the same thing.

Excellent! Thank you, Karl!

It never ceases to amaze me how long I continue to learn new
things about C. (Going on 16 years now.)
--
% Randy Yates % "Bird, on the wing,
%% Fuquay-Varina, NC % goes floating by
%%% 919-577-9882 % but there's a teardrop in his eye..."
%%%% <[email protected]> % 'One Summer Dream', *Face The Music*, ELO
http://home.earthlink.net/~yatescr
 
G

grahamo

Hi,

I think they way to interpret your const syntax is "the memory pointed
to by strLeft and strRight is constant". The pointers themselves are
not constant.

If you try

*strLeft = "blah";

then you'll see the compiler correctly catch your error for you.
Compare this declaration;

const char* str;

with

const char* const str;


const char* const foo = "hello"; // pointer to const data
const char* const foo = "hello"; // const pointer to const data, this
line of code wont compile

cout << foo << endl;
foo = "cool";
cout << foo << endl;
foo = "world";
cout << foo << endl;


G
 

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

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,512
Latest member
FinleyNick

Latest Threads

Top