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

confused between char and char* and connection to Arrays

P: n/a
i am trying to understand arrays and char. Stroustrup says, this a
string literal:

"this is a string literal"

he says it is of type /const char/. he also says, because of keeping
compatibilities with previous definitions of C & C++, it is also a
CHAR POINTER a.k.a /char*/. however it is an error, which can not be
caught at untill runtime, to modify a string literal using such
pointer:
char* name = "Plato" // name is an array of 5 char
name[0] = 'R' // error
tell me whether i am right or wrong

# 1 - p is an array of 6 CHARS (including '\0') , right ?

# 2 - p points to the 1st CHAR, 'P' of name a.ka. "Plato".

# 3 - it is *exactly same as /const char name[] = "Plato"/

#4 - what about /const char*[] = "Plato"/. what does this represent ?

Mar 29 '07 #1
Share this Question
Share on Google+
19 Replies


P: n/a
* arnuld:
i am trying to understand arrays and char. Stroustrup says, this a
string literal:

"this is a string literal"

he says it is of type /const char/.
I doubt he says that. The element type is 'const char'. The array is
of type 'char const [n]' where n is the number if characters in the
string literal + 1, the +1 for the terminating zero byte.

he also says, because of keeping
compatibilities with previous definitions of C & C++, it is also a
CHAR POINTER a.k.a /char*/.
I doubt he says that. The array is convertible to 'char*'.

however it is an error, which can not be
caught at untill runtime, to modify a string literal using such
pointer:
char* name = "Plato" // name is an array of 5 char
name[0] = 'R' // error
Yes.
tell me whether i am right or wrong

# 1 - p is an array of 6 CHARS (including '\0') , right ?
Where is the definition of 'p'?

Perhaps you mean 'name'.

'name' above is a pointer that points to the first element of an array
of 6 chars.

# 2 - p points to the 1st CHAR, 'P' of name a.ka. "Plato".
Assuming you mean 'name', yes.

# 3 - it is *exactly same as /const char name[] = "Plato"/
No.

#4 - what about /const char*[] = "Plato"/. what does this represent ?
That should not compile.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 29 '07 #2

P: n/a
On Mar 29, 1:28 pm, "Alf P. Steinbach" <a...@start.nowrote:
* arnuld:
"this is a string literal"
he says it is of type /const char/.

I doubt he says that. The element type is 'const char'. The array is
of type 'char const [n]' where n is the number if characters in the
string literal + 1, the +1 for the terminating zero byte.
you wording is clear(er) than him :-)

he also says, because of keeping
compatibilities with previous definitions of C & C++, it is also a
CHAR POINTER a.k.a /char*/.

I doubt he says that. The array is convertible to 'char*'.

it is not same but it is convertible. so where the difference between /
char const/ and /char*/ lies ?

tell me whether i am right or wrong
# 1 - p is an array of 6 CHARS (including '\0') , right ?

Where is the definition of 'p'?

Perhaps you mean 'name'.
yes, exactly, i apologize for putting confusion.

'name' above is a pointer that points to the first element of an array
of 6 chars.
thanks

# 3 - it is *exactly same as /const char name[] = "Plato"/

No.

i have already asked it: "where the difference lies ?"

#4 - what about /const char*[] = "Plato"/. what does this represent ?

That should not compile.
:-(, i am a poor programmer

thanks Alf

Mar 29 '07 #3

P: n/a
* arnuld:
>
>># 3 - it is *exactly same as /const char name[] = "Plato"/
No.


i have already asked it: "where the difference lies ?"
char const a[] = "Plato";
char const* p = "Pluto";

a is an array of six chars. sizeof(a) = 6 (or, in more detail, 6*1,
where 1 is the size of a char, which has size 1 per definition).

p is a pointer to the first element of an array of size chars.
sizeof(p) = the size of a pointer is on your system, probably 4, and it
depends not on the size of the array at all.

One thing you can do with the array and not with the pointer is to find
the number of elements:

size_t const aSize = sizeof(a)/sizeof(a[0]); // 6

Try the same with the pointer:

size_t const uhuh = sizeof(p)/sizeof(p[0]); // Size of a pointer.

(The reason the last expression evaluates to the size of a pointer is
that p[0] by definition means *(p+0) which by definition means *p which
is a char which by definition has size 1, so that sizeof(p[0]) is 1.)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 29 '07 #4

P: n/a
On 29 Mar, 09:38, "arnuld" <geek.arn...@gmail.comwrote:
On Mar 29, 1:28 pm, "Alf P. Steinbach" <a...@start.nowrote:
* arnuld:
# 3 - it is *exactly same as /const char name[] = "Plato"/
No.

i have already asked it: "where the difference lies ?"
char* name1 = "Plato";
const char name2[] = "Plato";

Well the fundamental difference is that name1 is of type pointer-to-
char and name2 is of type array-of-const-char. Arrays and pointers are
different things.

Another difference is that

*name1 = 'X'; will compile but the behaviour is undefined.
name2[0] = 'X'; will not compile because the chars in the array are
const.

This difference is not to do with arrays vs pointers, it is because,
as far as the compiler is concerned, name1 points to NON-CONST data
while name2 is an array of CONST data.

Gavin Deane

Mar 29 '07 #5

P: n/a
On Mar 29, 1:50 pm, "Alf P. Steinbach" <a...@start.nowrote:

char const a[] = "Plato";
char const* p = "Pluto";

a is an array of six chars. sizeof(a) = 6 (or, in more detail, 6*1,
where 1 is the size of a char, which has size 1 per definition).
that's clear now.

p is a pointer to the first element of an array of size chars.
sizeof(p) = the size of a pointer is on your system, probably 4, and it
depends not on the size of the array at all.

One thing you can do with the array and not with the pointer is to find
the number of elements:

size_t const aSize = sizeof(a)/sizeof(a[0]); // 6

Try the same with the pointer:

size_t const uhuh = sizeof(p)/sizeof(p[0]); // Size of a pointer.

(The reason the last expression evaluates to the size of a pointer is
that p[0] by definition means *(p+0) which by definition means *p which
is a char which by definition has size 1, so that sizeof(p[0]) is 1.)
i got it. "p" is a pointer to the 1st element of an array and "a" is
an array. hence we are dealing with 2 different "C++ types" exactly
like "int" and "char" are different.

BTW, tell me one thing. you used:

char const* p = "Pluto";

it is a "pointer" to a "const".

if you could have used: "char *const p" then "pointer" itself is a
constant.
Mar 29 '07 #6

P: n/a
On Mar 29, 1:57 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:
On 29 Mar, 09:38, "arnuld" <geek.arn...@gmail.comwrote:
On Mar 29, 1:28 pm, "Alf P. Steinbach" <a...@start.nowrote:
* arnuld:
# 3 - it is *exactly same as /const char name[] = "Plato"/
No.
i have already asked it: "where the difference lies ?"

char* name1 = "Plato";
const char name2[] = "Plato";

Well the fundamental difference is that name1 is of type pointer-to-
char and name2 is of type array-of-const-char. Arrays and pointers are
different things.

Another difference is that

*name1 = 'X'; will compile but the behaviour is undefined.
name2[0] = 'X'; will not compile because the chars in the array are
const.

This difference is not to do with arrays vs pointers, it is because,
as far as the compiler is concerned, name1 points to NON-CONST data
while name2 is an array of CONST data.

Gavin Deane
i got it, we are dealing with 2 DIFFERENT "C++ types"

Mar 29 '07 #7

P: n/a
arnuld wrote:
>On Mar 29, 1:50 pm, "Alf P. Steinbach" <a...@start.nowrote:

>char const a[] = "Plato";
char const* p = "Pluto";

a is an array of six chars. sizeof(a) = 6 (or, in more detail, 6*1,
where 1 is the size of a char, which has size 1 per definition).

that's clear now.

>p is a pointer to the first element of an array of size chars.
sizeof(p) = the size of a pointer is on your system, probably 4, and it
depends not on the size of the array at all.

One thing you can do with the array and not with the pointer is to find
the number of elements:

size_t const aSize = sizeof(a)/sizeof(a[0]); // 6

Try the same with the pointer:

size_t const uhuh = sizeof(p)/sizeof(p[0]); // Size of a pointer.

(The reason the last expression evaluates to the size of a pointer is
that p[0] by definition means *(p+0) which by definition means *p which
is a char which by definition has size 1, so that sizeof(p[0]) is 1.)

i got it. "p" is a pointer to the 1st element of an array and "a" is
an array. hence we are dealing with 2 different "C++ types"
Yes.
exactly like "int" and "char" are different.
I wouldn't say "exactly", since arrays can decay to pointers
and chars can't decay to ints:

voif foo(int[] param) {
cout << sizeof(param); // surprise: the size of a pointer
}

Admittedly, this is rather confusing --
-- C++ pretends you can pass an array to a function,
while you can actually pass a pointer only. The array
magically 'decays' to a pointer. So I would say that
arrays and pointers are more related than ints and chars.

HTH,
- J.
Mar 29 '07 #8

P: n/a
In article <11*********************@e65g2000hsc.googlegroups. com>,
"arnuld" <ge*********@gmail.comwrote:
On Mar 29, 1:50 pm, "Alf P. Steinbach" <a...@start.nowrote:

char const a[] = "Plato";
char const* p = "Pluto";

a is an array of six chars. sizeof(a) = 6 (or, in more detail, 6*1,
where 1 is the size of a char, which has size 1 per definition).

that's clear now.

p is a pointer to the first element of an array of size chars.
sizeof(p) = the size of a pointer is on your system, probably 4, and it
depends not on the size of the array at all.

One thing you can do with the array and not with the pointer is to find
the number of elements:

size_t const aSize = sizeof(a)/sizeof(a[0]); // 6

Try the same with the pointer:

size_t const uhuh = sizeof(p)/sizeof(p[0]); // Size of a pointer.

(The reason the last expression evaluates to the size of a pointer is
that p[0] by definition means *(p+0) which by definition means *p which
is a char which by definition has size 1, so that sizeof(p[0]) is 1.)

i got it. "p" is a pointer to the 1st element of an array and "a" is
an array. hence we are dealing with 2 different "C++ types" exactly
like "int" and "char" are different.

BTW, tell me one thing. you used:

char const* p = "Pluto";

it is a "pointer" to a "const".

if you could have used: "char *const p" then "pointer" itself is a
constant.
Right. What matters is which side of the '*' the 'const' is on. Some
people like to write "const char*" some people like to write "char
const*" but they are both the same thing and both are very different
than "char* const".
Mar 29 '07 #9

P: n/a
On Mar 29, 2:51 pm, Jacek Dziedzic
<jacek.dziedzic.n.o.s.p....@gmail.comwrote:
arnuld wrote:
On Mar 29, 1:50 pm, "Alf P. Steinbach" <a...@start.nowrote:
char const a[] = "Plato";
char const* p = "Pluto";
[...]
i got it. "p" is a pointer to the 1st element of an array and "a" is
an array. hence we are dealing with 2 different "C++ types"
Yes.
exactly like "int" and "char" are different.
I wouldn't say "exactly", since arrays can decay to pointers
and chars can't decay to ints:
The standard doesn't use the word decay. It says that an array
can convert implicitly to a pointer. And that a char can
convert implicitly to an int.

There is, of course, a difference: when you convert a char to
int, normally, no information is lost, and you can convert the
int back to a char, and get the same value. Whereas when you
convert an array to a pointer, you loose information, and you
can never go back.
voif foo(int[] param) {
cout << sizeof(param); // surprise: the size of a pointer
}
Admittedly, this is rather confusing --
-- C++ pretends you can pass an array to a function,
while you can actually pass a pointer only.
Thats a different issue. For reasons of C compatibility, "void
f(int param[])" is exactly the same type as "void f(int*
param)".

Where C compatibility isn't involved, this doesn't occur, and
you can write something like:
void f( int (&param)[ 25 ] )
and can only pass it an array of 25 ints (and not a pointer).
This is regularly used in things like:

template< typename T, size_t N >
T*
begin( T (&array)[ N ] )
{
return array ;
}

template< typename T, size_t N >
T*
end( T (&array)[ N ] )
{
return array + N ;
}

in order to get "iterators" into a C style array.
The array
magically 'decays' to a pointer. So I would say that
arrays and pointers are more related than ints and chars.
On the other hand, ints and chars are both integral types, with
the same set of operators defined over them, so they're strongly
related.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Mar 29 '07 #10

P: n/a
On Mar 29, 5:51 pm, Jacek Dziedzic
<jacek.dziedzic.n.o.s.p....@gmail.comwrote:
arnuld wrote:
On Mar 29, 1:50 pm, "Alf P. Steinbach" <a...@start.nowrote:
char const a[] = "Plato";
char const* p = "Pluto";
a is an array of six chars. sizeof(a) = 6 (or, in more detail, 6*1,
where 1 is the size of a char, which has size 1 per definition).
that's clear now.
p is a pointer to the first element of an array of size chars.
sizeof(p) = the size of a pointer is on your system, probably 4, and it
depends not on the size of the array at all.
One thing you can do with the array and not with the pointer is to find
the number of elements:
size_t const aSize = sizeof(a)/sizeof(a[0]); // 6
Try the same with the pointer:
size_t const uhuh = sizeof(p)/sizeof(p[0]); // Size of a pointer.
(The reason the last expression evaluates to the size of a pointer is
that p[0] by definition means *(p+0) which by definition means *p which
is a char which by definition has size 1, so that sizeof(p[0]) is 1.)
i got it. "p" is a pointer to the 1st element of an array and "a" is
an array. hence we are dealing with 2 different "C++ types"

Yes.
exactly like "int" and "char" are different.

I wouldn't say "exactly", since arrays can decay to pointers
and chars can't decay to ints:

voif foo(int[] param) {
cout << sizeof(param); // surprise: the size of a pointer

}

Admittedly, this is rather confusing --
-- C++ pretends you can pass an array to a function,
while you can actually pass a pointer only. The array
magically 'decays' to a pointer. So I would say that
arrays and pointers are more related than ints and chars.

hey J, i understood that

:-)

Mar 29 '07 #11

P: n/a
On Mar 29, 5:57 pm, "Daniel T." <danie...@earthlink.netwrote:

BTW, tell me one thing. you used:
char const* p = "Pluto";
it is a "pointer" to a "const".
if you could have used: "char *const p" then "pointer" itself is a
constant.

Right. What matters is which side of the '*' the 'const' is on. Some
people like to write "const char*" some people like to write "char
const*" but they are both the same thing and both are very different
than "char* const".
wait....

char const* p; // pointer to a constant
char *const p; // constant pointer to a char

char* const p;

aaaaahhhooooooo.....

WHAT IS THAT ?

Mar 29 '07 #12

P: n/a
On 29 Mar, 15:48, "arnuld" <geek.arn...@gmail.comwrote:
char *const p; // constant pointer to a char

char* const p;

aaaaahhhooooooo.....

WHAT IS THAT ?
Calm down, this one's easy :-)

They are the same as

char * const p;
char * const p;

They are all a const pointers to non-cost char. Just with different,
and equally allowable, use of whitespace. Nothing more.

Gavin Deane

Mar 29 '07 #13

P: n/a
On Mar 29, 8:25 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:
char *const p; // constant pointer to a char
aaaaahhhooooooo.....
WHAT IS THAT ?

Calm down, this one's easy :-)

They are the same as

char * const p;
char * const p;
They are all a const pointers to non-cost char. Just with different,
and equally allowable, use of whitespace. Nothing more.

my example was: "char* const p"

neither "char * const" nor "char *const p"

BTW, here is the confusion:

"char *const cp" -- const pointer to char [STROUSTRUP, 5.4.1]
"char* const cp" -- const pointer to char [FAQ]
which one i should believe ?

same for:

"char const* pc" -- pointer to const char [STROUSTRUP, 5.4.1]
"const Fred* p" -- p points to a Fred that is const [FAQ]
Mar 29 '07 #14

P: n/a
On 29 Mar, 17:17, "arnuld" <geek.arn...@gmail.comwrote:
On Mar 29, 8:25 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:
char *const p; // constant pointer to a char
aaaaahhhooooooo.....
WHAT IS THAT ?
Calm down, this one's easy :-)
They are the same as
char * const p;
char * const p;
They are all a const pointers to non-cost char. Just with different,
and equally allowable, use of whitespace. Nothing more.

my example was: "char* const p"

neither "char * const" nor "char *const p"
Yep. Those are all the same.
BTW, here is the confusion:

"char *const cp" -- const pointer to char [STROUSTRUP, 5.4.1]
"char* const cp" -- const pointer to char [FAQ]

which one i should believe ?
Believe both. And while you're at it, believe this too:

char * const cp -- const pointer to char [ME, JUST NOW]

How you arrange the * and any whitespace between the char and the
const is entirely up to you. It has no effect on the meaning.
same for:

"char const* pc" -- pointer to const char [STROUSTRUP, 5.4.1]
Yes. pc is a pointer to const char. Another way of saying the same
thing is that pc points to a char that is const.
"const Fred* p" -- p points to a Fred that is const [FAQ]
And here, p is a pointer to const Fred. Another way of saying the same
thing is that p points to a Fred that is const.

I'm not sure what your confusion is in this last example, so I'm not
sure if my comments help.

Gavin Deane

Mar 29 '07 #15

P: n/a
On Mar 29, 9:26 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:
arnuld wrote:
Yep. Those are all the same.
=:-0
BTW, here is the confusion:
"char *const cp" -- const pointer to char [STROUSTRUP, 5.4.1]
"char* const cp" -- const pointer to char [FAQ]
which one i should believe ?

Believe both. And while you're at it, believe this too:
char * const cp -- const pointer to char [ME, JUST NOW]
ok, i got it now. in all these 3 cases "*" always binds with "const".

"char const* pc" -- pointer to const char [STROUSTRUP, 5.4.1]
Yes. pc is a pointer to const char. Another way of saying the same
thing is that pc points to a char that is const.
"const Fred* p" -- p points to a Fred that is const [FAQ]

And here, p is a pointer to const Fred. Another way of saying the same
thing is that p points to a Fred that is const.

I'm not sure what your confusion is in this last example, so I'm not
sure if my comments help.
actually, confusion was everywhere and [ME, JUST NOW] helped a lot ;-)

thanks Gavin, i got it.

now i will write without looking at any FAQ, Stroustrup and your
comments:

char* const p == char *const p == char * const p

all of them said: "p is a constant pointer to a char"
char const* p == const char* p

both said: "p is pointer to const char"
right ?

(really i did not look at the book/FAQS/[ME, JUST NOW]

;-)
Mar 29 '07 #16

P: n/a
On 29 Mar, 17:41, "arnuld" <geek.arn...@gmail.comwrote:
On Mar 29, 9:26 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:
arnuld wrote:
Yep. Those are all the same.

=:-0
BTW, here is the confusion:
"char *const cp" -- const pointer to char [STROUSTRUP, 5.4.1]
"char* const cp" -- const pointer to char [FAQ]
which one i should believe ?
Believe both. And while you're at it, believe this too:
char * const cp -- const pointer to char [ME, JUST NOW]

ok, i got it now. in all these 3 cases "*" always binds with "const".
Yes. The whitespace around the * doesn't affect the meaning.

It has just occurred to me that maybe nobody has mentioned to you that
the trick with these is to read them right-to-left. I hope you have a
fixed pitch font...

char * const cp;
^ ^ ^ ^
4 3 2 1

Reading right-to-left: "cp [1] is a const [2] pointer [3] to char
[4]". The same works for

char* const cp;
^ ^ ^ ^
4 3 2 1

and

char *const cp;
^ ^ ^ ^
4 3 2 1

as long as you remember that the * is a separate entity in the
declaration syntax, regardless of whether it has whitespace on either
side or not.

<snip>
now i will write without looking at any FAQ, Stroustrup and your
comments:

char* const p == char *const p == char * const p

all of them said: "p is a constant pointer to a char"
Yep.
char const* p == const char* p

both said: "p is pointer to const char"

right ?
Yep.

Gavin Deane

Mar 29 '07 #17

P: n/a
On Mar 29, 9:59 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:
On 29 Mar, 17:41, "arnuld" <geek.arn...@gmail.comwrote:
Yes. The whitespace around the * doesn't affect the meaning.

It has just occurred to me that maybe nobody has mentioned to you that
the trick with these is to read them right-to-left. I hope you have a
fixed pitch font...

char * const cp;
^ ^ ^ ^
4 3 2 1

Reading right-to-left: "cp [1] is a const [2] pointer [3] to char
[4]". The same works for

char* const cp;
^ ^ ^ ^
4 3 2 1

and

char *const cp;
^ ^ ^ ^
4 3 2 1

as long as you remember that the * is a separate entity in the
declaration syntax, regardless of whether it has whitespace on either
side or not.

you are the 1st one who explained that "*" is a "separate identity". i
got it now. your 4-step process gives complete understanding.

char* const p == char *const p == char * const p
all of them said: "p is a constant pointer to a char"

Yep.
:-)
char const* p == const char* p
both said: "p is pointer to const char"
right ?

Yep.
:-)

Is Gavin Deane a good guy ?

yep

;-)

Mar 29 '07 #18

P: n/a
arnuld wrote:
On Mar 29, 1:28 pm, "Alf P. Steinbach" <a...@start.nowrote:
* arnuld:
"this is a string literal"
he says it is of type /const char/.
I doubt he says that. The element type is 'const char'. The array
is of type 'char const [n]' where n is the number if characters in
the string literal + 1, the +1 for the terminating zero byte.

you wording is clear(er) than him :-)
He says (TC++P3, pg 90):

quote:
The type of a string literal is "array of the appropriate number of
const characters", so "Bohr" is of type const char[5].
"Bohr" is the example literal on that page. That section very
thoroughly discusses the string literal. I don't see how you read that
and didn't understand. It looked very clear to me.

Brian
Mar 29 '07 #19

P: n/a
James Kanze wrote:
[snip]
Thanks for clarification!

- J.
Mar 30 '07 #20

This discussion thread is closed

Replies have been disabled for this discussion.