[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Different ways of declaring strings

bintom

10/5/2008 7:13:00 PM

What are the differences between the following methods of declaring
strings?

char string1[20] = "C++ forum";
char* string2 = "C++ forum";

I know that the first uses the array notation, whereas the second uses
pointer notation. But apart from that what are the implications /
dangers, etc. if any.
9 Answers

utab

10/5/2008 7:55:00 PM

0

On Sun, 05 Oct 2008 12:13:13 -0700, bintom wrote:

> What are the differences between the following methods of declaring
> strings?
>
> char string1[20] = "C++ forum";
> char* string2 = "C++ forum";
>
> I know that the first uses the array notation, whereas the second uses
> pointer notation. But apart from that what are the implications /
> dangers, etc. if any.

As an initial pointer for you, see the C FAQ

http://c-faq.com/decl/strli...

there are also other pointers there as well.

Gennaro Prota

10/5/2008 8:18:00 PM

0

bintom wrote:
> What are the differences between the following methods of declaring
> strings?
>
> char string1[20] = "C++ forum";
> char* string2 = "C++ forum";
>
> I know that the first uses the array notation, whereas the second uses
> pointer notation. But apart from that what are the implications /
> dangers, etc. if any.

It's not just notation: string1 is an array of chars, while string2 is a
pointer to char. A string-literal, such as "C++ forum", denotes an
object of static duration. For this reason there's no danger in writing
e.g.:

char const * f()
{
return "something" ;
}

but the following is a recipe for disaster:

char const * f()
{
char const arr[] = "something" ;
return arr ; // DON'T DO THIS!
}

In your first example you use the static duration object just as a
source to initialize string1 (in fact, it is likely that the compiler
will optimize away that source), whereas in the second one you directly
point to it (its first character, actually; and it may be the same
string literal object of the first line, or not; the implementation must
document this). Since attempting to modify the object corresponding to a
string literal yields undefined behavior you should add const:

char const * string2 = "C++ forum" ;

Omitting the const is allowed for backward-compatibility but deprecated.
Of course, you can modify string1, instead.

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/b...
Do you need expertise in C++? I'm available.

ebony.soft

10/6/2008 5:47:00 AM

0

On Oct 5, 10:13 pm, bintom <binoythomas1...@gmail.com> wrote:
> What are the differences between the following methods of declaring
> strings?
>
> char string1[20] = "C++ forum";
> char* string2 = "C++ forum";
>
> I know that the first uses the array notation, whereas the second uses
> pointer notation. But apart from that what are the implications /
> dangers, etc. if any.

Hi bintom
Besides what Gennaro wrote, I can add two other differences:
1. size of string1 is 20, so as Gennaro mentioned the 10 last array
elements fill
with null character, but the size of *string2 is 10.
2. string2 is a pointer and it can points to elsewhere, but string1
can't.

Regards,
Saeed Amrollahi

Maxim Yegorushkin

10/6/2008 8:20:00 AM

0

On Oct 5, 8:13 pm, bintom <binoythomas1...@gmail.com> wrote:
> What are the differences between the following methods of declaring
> strings?
>
> char string1[20] = "C++ forum";
> char* string2 = "C++ forum";
>
> I know that the first uses the array notation, whereas the second uses
> pointer notation. But apart from that what are the implications /
> dangers, etc. if any.

In the first case there is one object: an (initialised) array.

In the second case there are two objects: a string literal (which is a
char const[] array) and a pointer to it.

--
Max

Hendrik Schober

10/6/2008 9:00:00 AM

0

ebony.soft@gmail.com wrote:
> On Oct 5, 10:13 pm, bintom <binoythomas1...@gmail.com> wrote:
>> What are the differences between the following methods of declaring
>> strings?
>>
>> char string1[20] = "C++ forum";
>> char* string2 = "C++ forum";

<nitpick1>
This /defines/ strings.
</nitpick1>

> [...]
> 1. size of string1 is 20, so as Gennaro mentioned the 10 last array
> elements fill with null character, but the size of *string2 is 10.

<nitpick2>
The size of '*string2' is 1 (as it points to a 'char'). The size
of 'string2', OTOH, is the size of a 'char*' (4 on my system).
</nitpick2>

> [...]
> Saeed Amrollahi

Schobi

s_amrollahi

10/6/2008 10:47:00 AM

0

On Oct 6, 11:59 am, Hendrik Schober <spamt...@gmx.de> wrote:
> ebony.s...@gmail.com wrote:
> > On Oct 5, 10:13 pm, bintom <binoythomas1...@gmail.com> wrote:
> >> What are the differences between the following methods of declaring
> >> strings?
>
> >> char string1[20] = "C++ forum";
> >> char* string2 = "C++ forum";
>
>   <nitpick1>
>   This /defines/ strings.
>   </nitpick1>
>
> > [...]
> > 1. size of string1 is 20, so as Gennaro mentioned the 10 last array
> > elements fill with null character, but the size of *string2 is 10.
>
>   <nitpick2>
>   The size of '*string2' is 1 (as it points to a 'char'). The size
>   of 'string2', OTOH, is the size of a 'char*' (4 on my system).
>   </nitpick2>
>
> > [...]
> >   Saeed Amrollahi
>
>   Schobi

Sorry for some confusion. I mean in array case the size of string1 is
20, but in pointer case the size of
string literal which string2 points is 10 (9 bytes for "C++ forum" and
an additional byte
for '\0')

- Saeed Amrollahi

James Kanze

10/6/2008 12:38:00 PM

0

On Oct 5, 9:13 pm, bintom <binoythomas1...@gmail.com> wrote:
> What are the differences between the following methods of
> declaring strings?

> char string1[20] = "C++ forum";

This declares an array of char, initialized with
{
'C', '+', '+', ' ', 'f',
'o', 'r', 'u', 'm', '\0',
'\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0'
} ;

> char* string2 = "C++ forum";

This is really incorrect, and requires a deprecated conversion
in order for it to compile. It's the equivalent of declaring a
static char const hidden[ 10 ] =
{
'C', '+', '+', ' ', 'f',
'o', 'r', 'u', 'm', '\0'
} ;
and initializing string2 with its address.

Neither are used to declare "strings" in C++; one defines an
array of char, and the other a pointer to char. To declare a
string:
std::string string1( "C++ forum" ) ;
.

> I know that the first uses the array notation, whereas the
> second uses pointer notation. But apart from that what are the
> implications / dangers, etc. if any.

The implication is that they are two very different statements,
declaring different types of objects.

--
James Kanze (GABI Software) email:james.kanze@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

Gennaro Prota

10/6/2008 2:28:00 PM

0

James Kanze wrote:
> On Oct 5, 9:13 pm, bintom <binoythomas1...@gmail.com> wrote:
>> What are the differences between the following methods of
>> declaring strings?
>
>> char string1[20] = "C++ forum";
>
> This declares an array of char, initialized with
> {
> 'C', '+', '+', ' ', 'f',
> 'o', 'r', 'u', 'm', '\0',
> '\0', '\0', '\0', '\0', '\0',
> '\0', '\0', '\0', '\0', '\0'
> } ;

Adding some historical information (usually a special quality of
yours:-), the fact that the last elements are initialized to zero was
clarified with C90's TC2.

<http://www.open-std.org/jtc1/sc22/wg14/www/docs/t...

(the fix to subclause 6.5.7). C++03 isn't in sync (i.e., it leaves the
case as uncertain as the original ISO C did) and, last time I checked,
the working draft isn't, either.

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/b...
Do you need expertise in C++? I'm available.

blargg

10/6/2008 5:13:00 PM

0

In article
<823674df-6f7a-4153-9cd2-ef1129cdede8@r15g2000prd.googlegroups.com>,
Saeed Amrollahi <s_amrollahi@yahoo.com> wrote:

> On Oct 6, 11:59 am, Hendrik Schober <spamt...@gmx.de> wrote:
> > ebony.s...@gmail.com wrote:
> > > On Oct 5, 10:13 pm, bintom <binoythomas1...@gmail.com> wrote:
> > >> What are the differences between the following methods of declaring
> > >> strings?
> >
> > >> char string1[20] = "C++ forum";
> > >> char* string2 = "C++ forum";
> >
> >   <nitpick1>
> >   This /defines/ strings.
> >   </nitpick1>
> >
> > > [...]
> > > 1. size of string1 is 20, so as Gennaro mentioned the 10 last array
> > > elements fill with null character, but the size of *string2 is 10.
> >
> >   <nitpick2>
> >   The size of '*string2' is 1 (as it points to a 'char'). The size
> >   of 'string2', OTOH, is the size of a 'char*' (4 on my system).
> >   </nitpick2>
> >
> > > [...]
> > >   Saeed Amrollahi
> >
> >   Schobi
>
> Sorry for some confusion. I mean in array case the size of string1 is
> 20, but in pointer case the size of
> string literal which string2 points is 10 (9 bytes for "C++ forum" and
> an additional byte
> for '\0')

I disagree. For example,

const char* s1 = "012";
const char* s2 = "12";

s2 might equal s1+1 if the compiler shares the underlying character
array between the literals. In that case, s2 at the very least points to
the second element a 4-char array. I don't think you can really even
know the ultimate size of the underlying array (and thus what elements
can be accessed without invoking undefined behavior). There could even
be characters past the apparent end:

const char* s3 = "12\0Z"; //s3=s1+1