Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > confused between char and char* and connection to Arrays

Reply
Thread Tools

confused between char and char* and connection to Arrays

 
 
arnuld
Guest
Posts: n/a
 
      03-29-2007
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 ?

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-29-2007
* 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?
 
Reply With Quote
 
 
 
 
arnuld
Guest
Posts: n/a
 
      03-29-2007
> On Mar 29, 1:28 pm, "Alf P. Steinbach" <(E-Mail Removed)> 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 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

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-29-2007
* 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?
 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      03-29-2007
On 29 Mar, 09:38, "arnuld" <(E-Mail Removed)> wrote:
> > On Mar 29, 1:28 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> > * 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

 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      03-29-2007
> On Mar 29, 1:50 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:


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


 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      03-29-2007
On Mar 29, 1:57 pm, "Gavin Deane" <(E-Mail Removed)> wrote:
> On 29 Mar, 09:38, "arnuld" <(E-Mail Removed)> wrote:
>
> > > On Mar 29, 1:28 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> > > * 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"

 
Reply With Quote
 
Jacek Dziedzic
Guest
Posts: n/a
 
      03-29-2007
arnuld wrote:
>> On Mar 29, 1:50 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:

>
>
>> 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.
 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      03-29-2007
In article <(E-Mail Removed). com>,
"arnuld" <(E-Mail Removed)> wrote:

> > On Mar 29, 1:50 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:

>
>
> > 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".
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-29-2007
On Mar 29, 2:51 pm, Jacek Dziedzic
<(E-Mail Removed)> wrote:
> arnuld wrote:
> >> On Mar 29, 1:50 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:


> >> 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:(E-Mail Removed)
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments