confused between char and char* and connection to Arrays

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/. 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 ?
 
A

Alf P. Steinbach

* 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

arnuld

* 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 :)

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 ?

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
 
A

Alf P. Steinbach

* 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.)
 
G

Gavin Deane

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

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
 
A

arnuld

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.
 
A

arnuld

* 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"
 
J

Jacek Dziedzic

arnuld said:
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.
 
D

Daniel T.

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.[/QUOTE]

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".
 
J

James Kanze

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

[...]
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.
 
A

arnuld

arnuld said:
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

:)
 
A

arnuld

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 ?
 
G

Gavin Deane

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
 
A

arnuld

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]
 
G

Gavin Deane

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
 
A

arnuld

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]

;-)
 
G

Gavin Deane

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.

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
 
A

arnuld

On 29 Mar, 17:41, "arnuld" <[email protected]> wrote:
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.


:)

Is Gavin Deane a good guy ?

yep

;-)
 
D

Default User

arnuld said:
On Mar 29, 1:28 pm, "Alf P. Steinbach" <[email protected]> wrote:
* 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
 

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
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top