From: Keith Thompson on
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
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
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
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
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
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: C++ code for parsing syllables?
Next: memcpy