3

Please explain the difference between

char* str = "Hello";

And

char* str = {"Hello"};
0xdeadbeef
  • 86
  • 7
  • 2
    @EdHeal Haha.. Unfortunately this is not my homework... I am just a pathetic programmer staying up late at night wondering why on hell such things exist in C... – 0xdeadbeef May 10 '15 at 21:08
  • 2
    You really want to know? I tell you but don't tell anyone else: The variety of ways to do things in C allows people to express their individuality just like the snake skin jacket of the hero in that movie does... And our world needs real heroes. As we slowly learn to respect life of innocent snakes, we invented the C programming language. *nods* – BitTickler May 10 '15 at 21:18

3 Answers3

4

ISO 9899-1990 6.5.7 ("Initialization") says :

An array of character type may be initialized by a character string literal, optionally enclosed in braces.

aleroot
  • 71,077
  • 30
  • 176
  • 213
  • 1
    Sorry, maybe I understand something completely wrong, but there is no arrays here, only pointers. Also, this http://ideone.com/UKGfC3 (causes segfault) proves that second case is not an array. – HolyBlackCat May 10 '15 at 21:18
  • 2
    The string literal in ANSI C is a shortcut for an array of chars, doesn't it ? – aleroot May 10 '15 at 21:20
  • They are, but I see no *array initialization* here. Of course one can initialize a pointer with address of such string, but it is a *pointer initialization*. Both cases seem to do same thing, but syntax in the second one looks strange. – HolyBlackCat May 10 '15 at 21:23
1

There is no difference between those cases.

They both assign an address of string literal to a char pointer, but in second case an unusual syntax is used for it.

Similarly, int a = 42; and int a = {42}; are equivalent.


In the comments you've mentioned char *a = "Hello"; and char a[] = "Hello";.

They are completely different. Second one creates an array. It means same thing as

char a[] = {'H','e','l','l','o','\0'};

There is no number inside [] becase compiler can guess array's size for you (6 in this case).

And another case is completely different.

When you use a "string literal" outside of intialization of char array, like in this case

printf("Hello");

or

char *a = "Hello";

compiler implictly creates an array of const char to hold your string. As you know, in these contexts name of array decays to pointer to it's first element. So,

char *a = "Hello";

and

const char internal_array[] = "Hello";

char *a = internal_array; // same as  char *a = &internal_array[0];

are equivalent.

If you try to do something like

char *a = "Hello";
a[0] = 'A';

you will get a crash, because despite being a pointer to non-const char, a actually points to a constant string. Modifying it is not a good idea.


What about other case,

char a[] = "Hello";
a[0] = 'A';

is perfectly fine. In this case you get a new array of char that holds a string. Of course it's nonconst, so you can modify it.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • Okay i will confess something to you.. My doubt arose when i came across two kind of array initialization... Char a[]= "Hello"; and Char* a= "Hello".... Then while understanding the difference between them I stumbled across this gem... So i am further confused whether there is any special reason for this style of declaration.. – 0xdeadbeef May 10 '15 at 21:34
1

This one as I believe is a previously answered question. The link would be - Braces around string literal in char array declaration valid? (e.g. char s[] = {"Hello World"})

Both the declarations are the same. The answer to why it even existed is to provide some variety just to suit coders' tastes.(syntactic sugar) The only thing that I wanted to point out would be the char array variable declaration vs character pointer declaration. The pointers that you have defined have not been allocated any memory. Hence any edit/write operations on the string would lead to segmentation fault. Consider declaring

    char str[] = "Hello";

or

    char str[] = {"Hello"};
Community
  • 1
  • 1
Aditya Guru
  • 646
  • 2
  • 10
  • 18
  • And the reason why i could not edit the string if i follow character point declaration is not because it has not been allocated memory but on contrary it(string) is allocated memory on read-only part of data segment and the pointer variable is stored in the read-write part... – 0xdeadbeef May 10 '15 at 22:24
  • Yes, You are more precise! – Aditya Guru May 10 '15 at 22:29