T
thomas
char a[] = "abc";
char a[] = {'a','b','c','\0'};
Is there ANY difference between these two strings?
char a[] = {'a','b','c','\0'};
Is there ANY difference between these two strings?
char a[] = "abc";
char a[] = {'a','b','c','\0'};
Is there ANY difference between these two strings?
Maxim Yegorushkin said:char a[] = "abc";
char a[] = {'a','b','c','\0'};
Is there ANY difference between these two strings?
"abc" has the same binary layout as {'a','b','c','\0'} does, hence no
difference.
Why do you ask?
char a[] = "abc";
char a[] = {'a','b','c','\0'};Is there ANY difference between these two strings?
"abc" has the same binary layout as {'a','b','c','\0'} does, hence no
difference.
Why do you ask?
thomas said:On 24/10/09 10:58, thomas wrote:
char a[] = "abc";
char a[] = {'a','b','c','\0'};
Is there ANY difference between these two strings?
"abc" has the same binary layout as {'a','b','c','\0'} does, hence no
difference.
Why do you ask?
I thought the first may mean that the string cannot be modified. But
actually it can.
So I cannot figure out any difference between these two which I hope
exists.
In the first case you are not actually modifying the literal "abc" but
the values inside the 'a' array. That's different from:
const char* a = "abc";
In this case you don't have an array. You have a pointer pointing to a
literal. An modification attempt would modify the literal (and thus is
UB). In the case of the array you are modifying the contents of the
array, not the contents of the literal you used to initialize the array.
In the first case you are not actually modifying the literalthomas said:On Oct 24, 8:09 pm, Maxim Yegorushkin<[email protected]>
wrote:
On 24/10/09 10:58, thomas wrote:
char a[] = "abc";
char a[] = {'a','b','c','\0'};
Is there ANY difference between these two strings?
"abc" has the same binary layout as {'a','b','c','\0'} does, hence no
difference.
Why do you ask?
I thought the first may mean that the string cannot be
modified. But actually it can. So I cannot figure out any
difference between these two which I hope exists.
"abc" but the values inside the 'a' array. That's different
from:
const char* a = "abc";
In this case you don't have an array. You have a pointer
pointing to a literal. An modification attempt would modify
the literal (and thus is UB). In the case of the array you
are modifying the contents of the array, not the contents of
the literal you used to initialize the array.
A bit off topic, from unix linker point of view, char const[]
is better than char const* for global and namespace scope
strings.
This is because the latter refers to a string literal, so that
there are two objects: 1) char const* and 2) a string literal
to which 1) refers. In this case the (runtime) linker needs
to resolve the addresses of both objects. On the other hand,
when char const[] is initialized with a string literal, there
is only one object char const[] where that string literal is
actually stored, so there is one object to resolve for the
linker.
thomas wrote:
On Oct 24, 8:09 pm, Maxim Yegorushkin<[email protected]>
wrote:
On 24/10/09 10:58, thomas wrote:
char a[] = "abc";
char a[] = {'a','b','c','\0'};
Is there ANY difference between these two strings?
"abc" has the same binary layout as {'a','b','c','\0'} does, hence no
difference.
Why do you ask?
I thought the first may mean that the string cannot be
modified. But actually it can. So I cannot figure out any
difference between these two which I hope exists.
In the first case you are not actually modifying the literal
"abc" but the values inside the 'a' array. That's different
from:
const char* a = "abc";
In this case you don't have an array. You have a pointer
pointing to a literal. An modification attempt would modify
the literal (and thus is UB). In the case of the array you
are modifying the contents of the array, not the contents of
the literal you used to initialize the array.A bit off topic, from unix linker point of view, char const[]
is better than char const* for global and namespace scope
strings.
Every linker I've seen (Unix or otherwise) is capable of
handling both without any distinction.
Every linker I've seen (Unix or otherwise) is capable ofOn 24/10/09 18:59, Juha Nieminen wrote:
thomas wrote:
On Oct 24, 8:09 pm, Maxim Yegorushkin<[email protected]>
wrote:
On 24/10/09 10:58, thomas wrote:
char a[] = "abc";
char a[] = {'a','b','c','\0'};
Is there ANY difference between these two strings?
"abc" has the same binary layout as {'a','b','c','\0'}
does, hence no difference.
Why do you ask?
I thought the first may mean that the string cannot be
modified. But actually it can. So I cannot figure out
any difference between these two which I hope exists.
In the first case you are not actually modifying the
literal "abc" but the values inside the 'a' array. That's
different from:
const char* a = "abc";
In this case you don't have an array. You have a pointer
pointing to a literal. An modification attempt would
modify the literal (and thus is UB). In the case of the
array you are modifying the contents of the array, not the
contents of the literal you used to initialize the array.
A bit off topic, from unix linker point of view, char
const[] is better than char const* for global and namespace
scope strings.
handling both without any distinction.
Not true.
Here is an example:
[max@truth test]$ cat test.cc
char const a[] = "abc";
char const* b = "def";
char const* foo() { return a; }
char const* bar() { return b; }
Let's look at the assembly code of foo() and bar() when they
are compiled as position independent code (intended to be a
part of a shared library):
In summary, when building a shared library global and namespace scope
char const[] requires no run-time linker processing, where as char
const* does.
Making b char const* const instead, effectively making it
static, allows the compiler optimize out accessing b and
access the string referred by b directly.
The point I was making that using char const* for representing
global and namespace scope read-only stings is the worst
possible choice from the linker standpoint of view. Better
choices are char const[] or char const* const.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.