Prev: C++ code for parsing syllables?
Next: memcpy
From: Vitali Vinahradski on 22 Mar 2010 05:55 What is the difference between char *a="abc" and char b[]="abc"?
From: Francis Glassborow on 22 Mar 2010 06:11 Vitali Vinahradski wrote: > What is the difference between char *a="abc" and char b[]="abc"? The first defines a pointer and initialises it to point to a string literal, the second defines an array of sufficient size and then copies the string literal into that array. The most significant difference is that a[0] = 'A'; results in undefined behaviour (attempt to modify a string literal) but b[0] = 'A'; is fine.
From: Ben Bacarisse on 22 Mar 2010 07:12 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. -- Ben.
From: Vitali Vinahradski on 22 Mar 2010 07:57 Thanks a lot
From: Chameleon on 23 Mar 2010 10:16
στις 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. |