char * vs char[] 
Author Message
 char * vs char[]

I was just looking throught the sourcecode for 2 small projects
in the one, the author always used char[] for strings,
in the other they used char *
so what are the pros/cons on doing

char *a = "hello world";
vs
char b[] = "hello world";

besides using the sizeof operator on them ?



Fri, 14 Jan 2005 22:20:26 GMT  
 char * vs char[]

Quote:
> I was just looking throught the sourcecode for 2 small projects
> in the one, the author always used char[] for strings,
> in the other they used char *
> so what are the pros/cons on doing
> char *a = "hello world";
> vs
> char b[] = "hello world";
> besides using the sizeof operator on them ?

char *a="hello world" stores (or finds and uses) the literal string
"hello world" somewhere in some memory, and makes a point at its start.
You aren't guaranteed to be able to do anything to "hello world" other
than read it, but the pointer a is not bound to it and can be made to
point to anything else.
char b[]="hello world" reserves space for the array b and actually
writes "hello world" inside that array. Thus the "hello world" is
yours to do as you please, you can change it to your heart's contents.
However, b stays bound to the array, so you can't direct it anywhere
else.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"A bicycle cannot stand up by itself because it's two-tyred."
   - Sky Text



Fri, 14 Jan 2005 22:22:58 GMT  
 char * vs char[]
On Mon, 29 Jul 2002 14:20:26 GMT, Nils O. Sel?sdal said:

Quote:
> I was just looking throught the sourcecode for 2 small projects
> in the one, the author always used char[] for strings,
> in the other they used char *
> so what are the pros/cons on doing

> char *a = "hello world";

a can be made to point at another string, but the characters in
the current string are read-only. For example,
char *a = "hello, world";
char *b = "Bye bye world";
a = b; /* Allowed */

a[1] = 'u'; /* Not allowed */

Quote:
> vs
> char b[] = "hello world";

The characters of b can be changed, but b itself cannot be
changed to point at anotyher object.
char b[] = "hello, world";
char *a = "Bye bye world";
b = a; /* Not allowed */
b[0] = 'j'; /* Allowed */

If you expect to change neither the string contents, or the
physical pointer to the string, either is usable. Otherwise,
choose the one which doesn't have a constraint you can't live
with.

If you need the string to be both modifiable and changeable (if
you get my drift), you'll need to go the malloc() and strcpy()
route.

Cheers,
Dave.

--
           David Neary,
     E-Mail: bolsh at gimp dot org
CV: http://www.redbrick.dcu.ie/~bolsh/CV/CV.html



Fri, 14 Jan 2005 22:27:41 GMT  
 char * vs char[]

Quote:

> On Mon, 29 Jul 2002 14:20:26 GMT, Nils O. Sel?sdal said:
>> I was just looking throught the sourcecode for 2 small projects
>> in the one, the author always used char[] for strings,
>> in the other they used char *
>> so what are the pros/cons on doing

>> char *a = "hello world";

> a can be made to point at another string, but the characters in
> the current string are read-only. For example,
> char *a = "hello, world";
> char *b = "Bye bye world";
> a = b; /* Allowed */

> a[1] = 'u'; /* Not allowed */

>> vs
>> char b[] = "hello world";

> The characters of b can be changed, but b itself cannot be
> changed to point at anotyher object.
> char b[] = "hello, world";
> char *a = "Bye bye world";
> b = a; /* Not allowed */
> b[0] = 'j'; /* Allowed */

> If you expect to change neither the string contents, or the
> physical pointer to the string, either is usable. Otherwise,
> choose the one which doesn't have a constraint you can't live
> with.

> If you need the string to be both modifiable and changeable (if
> you get my drift), you'll need to go the malloc() and strcpy()
> route.

Not *need*, you could always get the best of both worlds, and stick with
automatic allocation, with something like:

char s_a[] = "hello, world";
char *s = s_a;

        - Kevin.

- Show quoted text -

Quote:

> Cheers,
> Dave.



Fri, 14 Jan 2005 22:38:02 GMT  
 char * vs char[]

Quote:
>I was just looking throught the sourcecode for 2 small projects
>in the one, the author always used char[] for strings,
>in the other they used char *
>so what are the pros/cons on doing

>char *a = "hello world";

Is a constant. "a" is a pointer to static data in the executable.

Quote:
>vs
>char b[] = "hello world";

doesnot seem to be a constant.


Fri, 14 Jan 2005 23:13:54 GMT  
 char * vs char[]


Quote:
>char *a="hello world" stores (or finds and uses) the literal string
>"hello world" somewhere in some memory, and makes a point at its start.

In other words, space for a pointer is allocated, as well as the space for
the string.  If this is the only instance of this string in your code, or
if your compiler doesn't pay attention to identical strings, then this
costs you the extra few bytes of memory to hold the pointer.  Not
necessarily a big deal, but...

Remember also that if you *do* change a to point to a different object,
*nothing* points to the string, and you can get the address back.
(Barring having saved it elsewhere first, but why?)

--
#include <standard.disclaimer>
 _
Kevin D Quitt  USA 91351-4454           96.37% of all statistics are made up
Per the FCA, this email address may not be added to any commercial mail list



Sat, 15 Jan 2005 05:00:49 GMT  
 char * vs char[]

Quote:



> >char *a="hello world" stores (or finds and uses) the literal string
> >"hello world" somewhere in some memory,
> >and makes a point at its start.

> In other words, space for a pointer is allocated,
> as well as the space for
> the string.  
> If this is the only instance of this string in your code, or
> if your compiler doesn't pay attention to identical strings, then this
> costs you the extra few bytes of memory to hold the pointer.  Not
> necessarily a big deal, but...

However...

    char *a="hello world";

merely initializes "a" with an address, while

    char a[] = "hello world";

initializes "a" with 12 bytes, which might be slower.

--
 pete



Sat, 15 Jan 2005 07:57:12 GMT  
 char * vs char[]

Quote:

>However...

>    char *a="hello world";

>merely initializes "a" with an address, while

>    char a[] = "hello world";

>initializes "a" with 12 bytes, which might be slower.

True, and in many implementations, the latter uses 12 bytes of init memory
(RAM or ROM) plus 12 bytes of RAM.  There you have it, sports fans: never
try to pick nits except on a specific beast.

--
#include <standard.disclaimer>
 _
Kevin D Quitt  USA 91351-4454           96.37% of all statistics are made up
Per the FCA, this email address may not be added to any commercial mail list



Sun, 16 Jan 2005 00:59:52 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. char[] vs char * vs #defines for constant strings

2. char ** vs char * f()

3. char [] vs char *

4. char ** VS char *** ???

5. const char * vs char const *

6. A char pointer (char *) vs. char array question

7. char *c vs. char c[]

8. char **str vs. char *str[]

9. char *line vs. char line[]

10. const char* vs. char *

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

12. char *Foo VS. char Bar[]

 

 
Powered by phpBB® Forum Software