char foo[]=""; vs char* foo=""; 
Author Message
 char foo[]=""; vs char* foo="";

Opinions on which of these is better/preferable/whatever:

    char  foo [] = "This is a test.";
    char* bar    = "This is a test.";

I realize, I think, most of the differences. (For instance, sizeof(foo)
is likely to be more useful than sizeof(bar). And, although bar takes
sizeof(char*) more memory than foo, it can be redirected to another
string value.)

I'm mostly interested in which y'all tend to use and why.

--

Engineering Information Services/Information Technology/3M, St.Paul, Minn
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"Rust Never Sleeps"



Thu, 29 Apr 1999 03:00:00 GMT  
 char foo[]=""; vs char* foo="";

Quote:

>Opinions on which of these is better/preferable/whatever:
>    char  foo [] = "This is a test.";
>    char* bar    = "This is a test.";
>I realize, I think, most of the differences. (For instance, sizeof(foo)
>is likely to be more useful than sizeof(bar). And, although bar takes
>sizeof(char*) more memory than foo, it can be redirected to another
>string value.)
>I'm mostly interested in which y'all tend to use and why.

If I want a constant string that will never change, I may use a pointer to
a string literal. Otherwise, you *must* use an array of char -- string
literals are (semantically) const. I always use:

        char const* bar = "This is a test.";
             ~~~~~

for pointers to string literals. The const helps keeps everybody honest...
-mark

--
---



Thu, 29 Apr 1999 03:00:00 GMT  
 char foo[]=""; vs char* foo="";

Quote:

>    char  foo [] = "This is a test.";
>    char* bar    = "This is a test.";
>I'm mostly interested in which y'all tend to use and why.

I use `char rcsid[]= "$Id$";' except on AIX, whose optimizing
linker yanks out unreferenced symbols.  Blah!

--
--Pierre Asselin, Westminster, Colorado




Thu, 29 Apr 1999 03:00:00 GMT  
 char foo[]=""; vs char* foo="";

Quote:

> Opinions on which of these is better/preferable/whatever:

>     char  foo [] = "This is a test.";
>     char* bar    = "This is a test.";

> I realize, I think, most of the differences. (For instance, sizeof(foo)
> is likely to be more useful than sizeof(bar). And, although bar takes
> sizeof(char*) more memory than foo, it can be redirected to another
> string value.)

> I'm mostly interested in which y'all tend to use and why.

Well, as a global variable, I'll rarely use the first form, since I can
simply use the string constant (possibly as a #define) and let the compiler
do the work of making sure they all point to one value (not guaranteed,
but likely on any decent compiler, and I usually don't care about bad
compilers).  If I have a global variable (where the memory issue might
be relevant, though still unlikely) it is most likely because I want to
be able to modify it.

For a local, the memory issue is irrelevant since if I don't modify it,
value propagation will probably eliminate the variable anyway, and we
are only talking about stack space, not real mem.  So the only time
I will use char x[] = ... is if I intend to use sizeof(x), and even
then I usually prefer strlen(FOO), since sizeof(x) is tricky to use
(and IMO confusing to read) because of the '\0'.

---------------------------------------------------------------------------
Tim Hollebeek         | Disclaimer :=> Everything above is a true statement,
Electron Psychologist |                for sufficiently false values of true.

----------------------| http://wfn-shop.princeton.edu/~tim (NEW! IMPROVED!)



Sat, 01 May 1999 03:00:00 GMT  
 char foo[]=""; vs char* foo="";

Quote:


> >    char  foo [] = "This is a test.";
> >    char* bar    = "This is a test.";

> >I'm mostly interested in which y'all tend to use and why.

> I use `char rcsid[]= "$Id$";' except on AIX, whose optimizing
> linker yanks out unreferenced symbols.  Blah!

make it static and AIX will keep it there:

static char sccsid[] = "$Id$";

works for me.

.bri.

Quote:
> --
> --Pierre Asselin, Westminster, Colorado



--
`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'

RS/6000 Division                           VNET:     BRIANH at AUSTIN
11400 Burnet Rd, 4449/045-2L007            internal:

Austin, TX  78758                   Phone: (512) 838-2958  t/l 678-2958
`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'


Wed, 05 May 1999 03:00:00 GMT  
 char foo[]=""; vs char* foo="";



Quote:
>Opinions on which of these is better/preferable/whatever:

>    char  foo [] = "This is a test.";
>    char* bar    = "This is a test.";

>I realize, I think, most of the differences. (For instance, sizeof(foo)
>is likely to be more useful than sizeof(bar). And, although bar takes
>sizeof(char*) more memory than foo,

No, bar probably (almost certainly) takes less memory.  Both will have
the string embedded in the code image (CODE segment).  The first will
have to allocate 16 bytes in the data area (stack, for example) for the
foo array.  The second will probably only need to allocate a few bytes
for the pointer, which then points to the string in the CODE segment.

Quote:
>it can be redirected to another string value.)

But so can foo, really.  It's just that one is changed with strcpy and
the other with =.

Quote:

>I'm mostly interested in which y'all tend to use and why.

If you intend to alter the string at any stage, use "char foo[]",
otherwise, use "char* bar".  Actually, in the latter case, you should
write

    const char* bar = "This is a test.";

since strings should be assumed to be const arrays.  The pointer should
preserve this const-ness.  If intend bar to only ever point to the same
string, then you should actually declare as:

    const char* const bar = "This is a test.";

Which prevents (possibly) illegal writes to read-only memory AND
prevents bar from being accidentally re-assigned to something else.

============================================================================
 Ian Cargill  CEng MIEE  | Find out about the Association of C and C++ Users



Wed, 05 May 1999 03:00:00 GMT  
 char foo[]=""; vs char* foo="";





 >>Opinions on which of these is better/preferable/whatever:
 >>
 >>    char  foo [] = "This is a test.";
 >>    char* bar    = "This is a test.";
 >>
 >>I realize, I think, most of the differences. (For instance, sizeof(foo)
 >>is likely to be more useful than sizeof(bar). And, although bar takes
 >>sizeof(char*) more memory than foo,
 >
 >No, bar probably (almost certainly) takes less memory.  Both will have
 >the string embedded in the code image (CODE segment).  The first will
 >have to allocate 16 bytes in the data area (stack, for example) for the
 >foo array.  The second will probably only need to allocate a few bytes
 >for the pointer, which then points to the string in the CODE segment.

That assumes that foo and bar are defined as auto variables.
If they are external variables then things are likely to be as Chris
describes.

 >>it can be redirected to another string value.)
 >
 >But so can foo, really.  It's just that one is changed with strcpy and
 >the other with =.

Of course you can't put a string of more than 15 characters (plus
null terminator) in foo whereas bar can reference any string.

 >>I'm mostly interested in which y'all tend to use and why.
 >
 >If you intend to alter the string at any stage, use "char foo[]",
 >otherwise, use "char* bar".  Actually, in the latter case, you should
 >write
 >
 >    const char* bar = "This is a test.";
 >
 >since strings should be assumed to be const arrays.  The pointer should
 >preserve this const-ness.  If intend bar to only ever point to the same
 >string, then you should actually declare as:
 >
 >    const char* const bar = "This is a test.";

That makes having the pointer pointless(!) unless you have some strange
code that works through a (const char *const *) pointer. It is a recipe
for pointer type incompatibilities.

--
-----------------------------------------


-----------------------------------------



Thu, 06 May 1999 03:00:00 GMT  
 char foo[]=""; vs char* foo="";

Quote:





>  >>Opinions on which of these is better/preferable/whatever:

>  >>    char  foo [] = "This is a test.";
>  >>    char* bar    = "This is a test.";

>  >foo array.  The second will probably only need to allocate a few bytes
>  >for the pointer, which then points to the string in the CODE segment.

> That assumes that foo and bar are defined as auto variables.
> If they are external variables then things are likely to be as Chris
> describes.

        Depends on the environment. On a system with a loader, yes,
this is true. On embedded systems with code in ROM, the char foo[]
type of definition requires almost double the storage space as for
char *foo. Essentially, there would be a copy of the string in ROM code,
which a built-in loader would then copy to the RAM area assigned to
array foo. For the char *bar type of declaration, this copy is not
required - bar can directly point to the ROM-based string. Obviously,
the extra storage associated with char foo[] provides the added
flexibility of modifying the string at run-time.

Quote:

>  >>it can be redirected to another string value.)

>  >string, then you should actually declare as:

>  >    const char* const bar = "This is a test.";
> That makes having the pointer pointless(!)

        Mostly true. However, there are compilers which do not merge
duplicate strings. In such cases, if all you want is to pass the pointer
down to second-level functions, this is the definition that best
captures the semantics. Of course, pointer incompatibility warnings
are very likely (str... functions take char * arguments, not const
char * ones) - however, not that it is the first const that causes
the incompatibility, the second const would generally not be an issue,
unless you meddle with &bar.

-----------
/* Is this really comp.lang.c.moderated? */

/* Ajoy K Thamattoor, "aka Ajoy Krishnan T, to those who remember me",
   MTS-1,
   Lucent Bell Labs,
   67 Whippany Road, Whippany,
   New Jersey, US */



Received: from xcski.com by relay3.UU.NET with SMTP
        (peer crosschecked as: compass.roc.servtech.com [204.181.4.141])
        id QQbqnp28785; Tue, 19 Nov 1996 13:27:27 -0500 (EST)

Date: Tue, 19 Nov 1996 13:27:11 -0500



Status: R

Newsgroups: comp.lang.c.moderated
Path: ptomblin

Subject: Re: printf 9999.6 -> 1000 - bug?


Organization: Tomblin Computer Consulting, Rochester, New York and Ottawa, Ontario


Date: Tue, 19 Nov 1996 18:27:07 GMT


Quote:


>>>gives an interesting example of rounding: 9999.6 = approx. 1000.

>>Any other rounding would be a bug.

>Hear, hear.  The "correct" rounding algorithm produces a result which is
>one order of magnitude lower than the number being rounded.  What colour
>is the sky on your planet?

It's a kind of mauvy pink, except when it's more pinky mauve.

I assumed the order of magnitude error was in transcribing, not in the output.
I find a bug like that in a library routine hard to believe, even from
Borland.  But then again, I didn't believe a math chip could make mistakes in
just some numbers, and not all numbers involving a particular bit, so what do
I know.

--
Paul Tomblin, PP-ASEL         _|_        Rochester Flying Club web page:
                         ____/___\____   http://www.servtech.com/public/
                  ___________[o0o]___________   ptomblin/rfc.html



Sat, 08 May 1999 03:00:00 GMT  
 char foo[]=""; vs char* foo="";

Quote:
>  If you intend to alter the string at any stage, use "char foo[]",
>  otherwise, use "char* bar".  Actually, in the latter case, you should
>  write
>      const char* bar = "This is a test.";
>  since strings should be assumed to be const arrays.  The pointer should
>  preserve this const-ness.  If intend bar to only ever point to the same
>  string, then you should actually declare as:
>      const char* const bar = "This is a test.";
>  Which prevents (possibly) illegal writes to read-only memory AND
>  prevents bar from being accidentally re-assigned to something else.

In the latter case, what you probably want, which DOES take less memory,
is:

        static const char bar[] = "This is a test";

This doesn't use stack space or a pointer.  It just uses the space in
the CODE or TEXT segment to store the actual string.

-Geoff

Full Name: Geoffrey Odhner

Office:    Franklin Electronic Publishers, Inc.
           One Franklin Plaza
           Burlington, NJ 08016-4907
    Voice: (609) 386-2500 (ext 4880)
    Fax:   (609) 387-0649
    Fax:   (609) 387-1870



Mon, 10 May 1999 03:00:00 GMT  
 char foo[]=""; vs char* foo="";

Quote:
> >  If you intend bar to only ever point to the same
> >  string, then you should actually declare as:
> >      const char* const bar = "This is a test.";
> >  Which prevents (possibly) illegal writes to read-only memory AND
> >  prevents bar from being accidentally re-assigned to something else.
> In the latter case, what you probably want, which DOES take less memory,
> is:
>    static const char bar[] = "This is a test.";

(I've inserted the missing period before the last close-quote.)

Besides the matter of "static", there are two significant differences
between these declarations.  With the first one, sizeof bar yields
sizeof(char *).  With the second, sizeof bar yields the same value as
strlen(bar)+1.  This can be an advantage for the second version if you
actually want that informatiOn.

The other difference is that with the first version, the string literal
is allowed to share space with other string literals that are identical
or where one matches a substring at the end of the other.  Thus if you
continue with:

        const char* const bar2 = "a test.";
        const char* const bar3 = "This is a test.";

then the only storage allocated may be the two additional pointers.
bar and bar3 can be equal, and bar2 can equal bar+8, and you end up
saving a little memory.  Note that the compiler is not required to
consolidate strings in this way, though; it's just *allowed* to if it
wants.  But if you declare explicit arrays, using [], the consolidation
is *not allowed*.

(Note: the original standard did not explicitly say that bar2 could
 equal bar+8; this was made explicit as part of Technical Corrigendum 2.)

Still another option with a constant string is to use a string literal
directly, or to obtain this effect by doing:

        #define bar "This is a test."

This behaves like the second version with respect to sizeof, but like
the first version with respect to multiple instances being consolidated
or not.  It has the disadvantage that you don't get the compiler's
protection against inadvertent modification that const would give you,
so you can accidentally invoke undefined behavior by doing something
like "bar[0] = 'W'; /* WRONG */".  This method is not usually what you
want, but I mention it for completeness.
--

SoftQuad Inc., Toronto        is always right"           -- Michael DeCorte

My text in this article is in the public domain.



Thu, 13 May 1999 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. extern char *foo vs. extern char foo[]

2. OT: Origination of "foo"

3. What is "foo"???

4. "foo" origin

5. "foo" origin

6. #ifdef "foo"

7. Text Mappings like _T("foo")

8. #include "foo" vs #include <foo>

9. #include "foo" vs #include <foo>

10. char a[] = "abc"; char* a = "abc";

11. Difference between "char *arr" and "char arr[]"

12. char *Foo VS. char Bar[]

 

 
Powered by phpBB® Forum Software