Author |
Message |
Nils O. Sel?sd #1 / 8
|
 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 |
|
 |
Joona I Palast #2 / 8
|
 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 |
|
 |
Dave Near #3 / 8
|
 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 |
|
 |
Kevin Easto #4 / 8
|
 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. Quote:
|
Fri, 14 Jan 2005 22:38:02 GMT |
|
 |
Jan Engelhard #5 / 8
|
 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 |
|
 |
Kevin D. Quit #6 / 8
|
 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 |
|
 |
pete #7 / 8
|
 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 |
|
 |
Kevin D. Quit #8 / 8
|
 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 |
|
|
|