From: Vitali Vinahradski on
What is the difference between char *a="abc" and char b[]="abc"?
From: Francis Glassborow on
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
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
Thanks a lot
From: Chameleon on
στις 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.
 |  Next  |  Last
Pages: 1 2 3
Prev: C++ code for parsing syllables?
Next: memcpy