Reputation: 2183
What is the difference between
char ch [ ] = "hello";
and
char ch [ ] = { 'h','e','l','l','o','\0'};
and why we can only do
char *p = "hello";
But cant do
char *p = {'h','e','l','l','o','\0'};
Upvotes: 3
Views: 96
Reputation: 2367
The two array declarations are the same. As for the pointer declaration, the form char *p = {'h','e','l','l','o','\0'};
is not valid, simply because it was not included in the compiler design.
There is no theoretical reason, in my knowledge, why that declaration should not be valid, when char *p = "hello";
is.
Upvotes: 2
Reputation: 9680
Let's take it one by one. This following is initializing a char
array ch
using the string literal "hello"
.
char ch[] = "hello";
The following is initializing the array ch
using an array initialization list. This is equivalent to the above statement.
char ch[] = {'h', 'e', 'l', 'l', 'o', '\0'};
The following is initializing a char
pointer p
to point to the memory where the string literal "hello"
is stored. This is read-only memory. Attempting to modify its contents will not give compile error because string literal in C
are not const qualified unlike in C++
, but will cause undefined behaviour or even program crash.
char *p = "hello";
const char *p = "hello"; // better
The following last statement is plain wrong.
char *p = {'h','e','l','l','o','\0'};
p
is a char
pointer here, not an array and can't be initialized using array initialization list. I have highlighted the words array
and pointer
above to emphasize that array and pointer are different types. In some cases, an array is implicitly converted to a pointer to its first element like when an array is passed to a function or assigned to a pointer of the same type. This does not mean they are the same. They have different pointer arithmetic and different sizeof
values.
Upvotes: 1
Reputation: 60037
Open upon a time they made a mistake about const
The bit "hello" should be a const char * const
but they where lazy and just used char *
. But to keep the faith alive they let that one slip.
Then they said. Ok. They can be equal.
Then they had char [] = { 'a', 'b', ...};
and all was good in the world
The the evil monster came and thrust upon them char *p = "hello"
. But the evil monster was in a good mood and said it should be const char *p = "hello"
but I would be happy with that.
He went home and but the evil monster was not amused. He dictated over his realm char *p = {'h','e','l','l','o','\0'};
is the sign of a heretic.
Basically there was a cock-up. Just do it right from now and there is old code kicking around that needs to be satisfied.
Upvotes: 1
Reputation: 171
There is no difference in
char ch [ ] = "hello";
char ch [ ] = { 'h','e','l','l','o','\0'};
But check below
char *p = "hello"; //This is correct
char *p = {'h','e','l','l','o','\0'}; //This is wrong
If you want to make it correct you need to use
char *p[]={'h','e','l','l','o','\0'}; //This works
Upvotes: -2
Reputation: 145899
char ch [ ] = "hello";
char ch [ ] = { 'h','e','l','l','o','\0'};
There is no difference. ch
object will be exactly the same in both declarations.
On why we cannot do:
char *p = {'h','e','l','l','o','\0'};
An initializer list of more than one value can only be used for objects of aggregate type (structures or array). You can only initialize a char *
with a pointer value.
Actually:
char ch [ ] = "hello";
and
char *p = "hello";
are not the same. The first initializes an array with the elements of a string literal and the second is a pointer to a string literal.
Upvotes: 3