Prev: C++ code for parsing syllables?
Next: memcpy
From: Keith Thompson on 23 Mar 2010 11:20 Chameleon <gessos.paul(a)yahoo.gr> writes: > στις 22 Μαρ 2010 13:12, O/H Ben Bacarisse έγραψε: >> Vitali Vinahradski<connormclaud(a)gmail.com> writes: >> >>> What is the difference between char *a="abc" and char b[]="abc"? >> >> Another difference that might be worth noting is that if these appear >> in a function, that function might reasonably return a, but a function >> that declares and returns b is an error waiting to happen. >> >> In the first case, what is returned is a pointer to a statically >> allocated string whose lifetime is the whole execution time of the >> program; whereas in the second a pointer to automatic storage is >> returned -- storage that whose lifetime ends as soon as the function >> returns. >> > > whow!!! > For 10 years I was believe that it was exactly the same thing!!! > > > Learning till death. Then section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>, was written just for you. -- Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
From: AnonMail2005 on 29 Mar 2010 11:59 On Mar 22, 5:55 am, Vitali Vinahradski <connormcl...(a)gmail.com> wrote: > What is the difference between char *a="abc" and char b[]="abc"? Note also that the former takes up more storage space. *a allocates space for the characters (including an ending null) plus space for a pointer. b[] just allocates space for the characters. HTH
From: Francis Glassborow on 7 Apr 2010 04:26 AnonMail2005(a)gmail.com wrote: > On Mar 22, 5:55 am, Vitali Vinahradski <connormcl...(a)gmail.com> wrote: >> What is the difference between char *a="abc" and char b[]="abc"? > > Note also that the former takes up more storage space. *a allocates > space for the characters (including an ending null) plus space for a > pointer. b[] just allocates space for the characters. > > HTH Well I suppose it might, but it also might not. Consider: void foo(){ char *a = "abc"; char b[] = "abc"; .... } The compiler allocates storage for a and initialises it to point to the literal "abc". Then it allocates storage for and array of four characters and initialises it by copying the string literal (which could be the same string literal as that pointed to by a but is not required to be. Assertions as to what compilers in general will do by way of storage allocation are almost always wrong. Even assertions about what a single compiler will do (with a specific set of compile switches) can be mistaken as the compiler might be using context from elsewhere in the code.
From: AnonMail2005 on 9 Apr 2010 09:36 On Apr 7, 4:26 am, Francis Glassborow <francis.glassbo...(a)btinternet.com> wrote: > AnonMail2...(a)gmail.com wrote: > > On Mar 22, 5:55 am, Vitali Vinahradski <connormcl...(a)gmail.com> wrote: > >> What is the difference between char *a="abc" and char b[]="abc"? > > > Note also that the former takes up more storage space. *a allocates > > space for the characters (including an ending null) plus space for a > > pointer. b[] just allocates space for the characters. > > > HTH > > Well I suppose it might, but it also might not. Consider: > > void foo(){ > char *a = "abc"; > char b[] = "abc"; > ... > > } > > The compiler allocates storage for a and initialises it to point to the > literal "abc". Then it allocates storage for and array of four > characters and initialises it by copying the string literal (which could > be the same string literal as that pointed to by a but is not required > to be. > > Assertions as to what compilers in general will do by way of storage > allocation are almost always wrong. Even assertions about what a single > compiler will do (with a specific set of compile switches) can be > mistaken as the compiler might be using context from elsewhere in the code. I guess I was being a bit too specific. The only point I was trying to make was that the pointer is a variable which has space allocated for it separate from what it points to. While the array and it's contents are one and the same storage location. Hope that clarifies it.
From: Ali Karaali on 24 Apr 2010 12:21
On 7 Nisan, 11:26, Francis Glassborow <francis.glassbo...(a)btinternet.com> wrote: > AnonMail2...(a)gmail.com wrote: > > On Mar 22, 5:55 am, Vitali Vinahradski <connormcl...(a)gmail.com> wrote: > >> What is the difference between char *a="abc" and char b[]="abc"? > > > Note also that the former takes up more storage space. *a allocates > > space for the characters (including an ending null) plus space for a > > pointer. b[] just allocates space for the characters. > > > HTH > > Well I suppose it might, but it also might not. Consider: > > void foo(){ > char *a = "abc"; > char b[] = "abc"; > ... > > } > > The compiler allocates storage for a and initialises it to point to the > literal "abc". Then it allocates storage for and array of four > characters and initialises it by copying the string literal (which could > be the same string literal as that pointed to by a but is not required > to be. After copied the string literal, What is happening? The string literal still lives or what? If so, the program will have 2*4 characters since string literal lives whole execution time. One of them string literal, other one, the array. Do you see my point? > > Assertions as to what compilers in general will do by way of storage > allocation are almost always wrong. Even assertions about what a single > compiler will do (with a specific set of compile switches) can be > mistaken as the compiler might be using context from elsewhere in the code. Ali |