7

In C/C++, what's the difference between the following two line code:

char *str1="hello";  
char *str2={"hello"};  
user229044
  • 232,980
  • 40
  • 330
  • 338
yejinxin
  • 586
  • 1
  • 3
  • 13
  • 1
    no difference. they both break the compilation – BЈовић Oct 05 '12 at 13:02
  • 3
    @BЈовић At least in C++, but not in C (and who knows what *"C/C++"* is). – Christian Rau Oct 05 '12 at 13:09
  • @ChristianRau - I know exactly what "C/C++" means, and I suspect that you do, too. – Pete Becker Oct 05 '12 at 13:31
  • 1
    @PeteBecker The only thing a question “X in C/C++?” can possibly mean is “X in C? X in C++?”. This is bad because it is bad to ask several questions in the same question. At the time of this comment, there is an answer that applies only to C++ and another that applies only to C. http://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts – Pascal Cuoq Oct 05 '12 at 14:08
  • 1
    @PeteBecker Of course I know it, the sarcasm was intentional. And in fact this question is one of the instances where *C/C++* is totally inappropriate (though I agree it can sometimes be appropriate, but often it is not, because the OP usually isn't aware of its implications), since the answer is different for both languages. I understand the answers are often the same for C and C++ (at least for pure language and standard related questions), but here they definitely aren't. – Christian Rau Oct 05 '12 at 14:16
  • @BЈовић: Please explain how this is supposed to break the compilation. O.o – netcoder Oct 05 '12 at 20:24
  • @netcoder `char *str1="hello";` doesn't compile for c++ – BЈовић Oct 05 '12 at 20:33
  • 1
    @BЈовић: That actually doesn't break compilation. It emits a warning in C++ (`write-strings` IIRC) and compiles just fine in C. – netcoder Oct 06 '12 at 01:12
  • @netcoder not with -Werror. anyway, would you ignore a warning? – BЈовић Oct 06 '12 at 11:14
  • @BЈовић: No I wouldn't. It's deprecated and it's discouraged, yes you're right on that. I was just pointing out that it doesn't break compilation per se, most likely for compatibility reasons with C. :) – netcoder Oct 06 '12 at 14:43

4 Answers4

10

Per the 2011 C standard, clause 6.7.9 Initialization, paragraph 11: “The initializer for a scalar shall be a single expression, optionally enclosed in braces…”

That is it. There is no semantic difference; the braces simply may be present or may be absent, with no change to the meaning.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
6

Style only in this case. They both result in the same thing, and they're both bad form. You should have used const char * str1="hello";.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • {"hello"} isn't it consider as array? as we know to define the array we write in {} m just confuse about this and what if char *str2={"hello","hi"}; in this case – Pratik Oct 05 '12 at 13:04
  • @JensGustedt Would please tell what's the definition of scalar types? – yejinxin Oct 05 '12 at 13:24
  • @yejinxin, this is really a basic concept in C, these are the integral types, floating types and pointer types – Jens Gustedt Oct 05 '12 at 13:36
  • @JensGustedt, well, I guess my English is too poor... I know it now. – yejinxin Oct 05 '12 at 13:40
2

See https://stackoverflow.com/a/3462768/153225.

The braces are redundant.

Generating assembler form the following code with "gcc -S" confirms that they generate exactly the same thing (with a slightly different constant in each case):

#include <iostream>
using namespace std;

void test1() {
    const char *str1="hello1";
    cout << str1 << endl;
}

void test2() {
    const char *str2={"hello2"};
    cout << str2 << endl;
}

int main() {
    test1();
    test2();
}
Community
  • 1
  • 1
Martin
  • 3,703
  • 2
  • 21
  • 43
-1

There is no difference between an array and a "string", due a string is an array of characters.

adripanico
  • 1,026
  • 14
  • 32