What is the difference between char a[] = ?string?; and char *p = ?string?;?

前端 未结 8 1012
太阳男子
太阳男子 2020-11-22 07:43

As the heading says, What is the difference between

char a[] = ?string?; and 
char *p = ?string?;  

This question was asked to me in inter

8条回答
  •  既然无缘
    2020-11-22 08:15

    The ? seems to be a typo, it is not semantically valid. So the answer assumes the ? is a typo and explains what probably the interviewer actually meant to ask.


    Both are distinctly different, for a start:

    1. The first creates a pointer.
    2. The second creates an array.

    Read on for more detailed explanation:

    The Array version:

    char a[] = "string";  
    

    Creates an array that is large enough to hold the string literal "string", including its NULL terminator. The array string is initialized with the string literal "string". The array can be modified at a later time. Also, the array's size is known even at compile time, so sizeof operator can be used to determine its size.


    The pointer version:

    char *p  = "string"; 
    

    Creates a pointer to point to a string literal "string". This is faster than the array version, but string pointed by the pointer should not be changed, because it is located in a read only implementation-defined memory. Modifying such an string literal results in Undefined Behavior.

    In fact C++03 deprecates[Ref 1] use of string literal without the const keyword. So the declaration should be:

    const char *p = "string";
    

    Also,you need to use the strlen() function, and not sizeof to find size of the string since the sizeof operator will just give you the size of the pointer variable.


    Which version is better and which one shall I use?

    Depends on the Usage.

    • If you do not need to make any changes to the string, use the pointer version.
    • If you intend to change the data, use the array version.

    Note: This is a not C++ but this is C specific.

    Note that, use of string literal without the const keyword is perfectly valid in C. However, modifying a string literal is still an Undefined Behavior in C[Ref 2].

    This brings up an interesting question,
    What is the difference between char* and const char* when used with string literals in C?


    For Standerdese Fans:
    [Ref 1]C++03 Standard: §4.2/2

    A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either case, the result is a pointer to the first element of the array. This conversion is considered only when there is an explicit appropriate pointer target type, and not when there is a general need to convert from an lvalue to an rvalue. [Note: this conversion is deprecated. See Annex D. ] For the purpose of ranking in overload resolution (13.3.3.1.1), this conversion is considered an array-to-pointer conversion followed by a qualification conversion (4.4). [Example: "abc" is converted to “pointer to const char” as an array-to-pointer conversion, and then to “pointer to char” as a qualification conversion. ]

    C++11 simply removes the above quotation which implies that it is illegal code in C++11.

    [Ref 2]C99 standard 6.4.5/5 "String Literals - Semantics":

    In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence; for wide string literals, the array elements have type wchar_t, and are initialized with the sequence of wide characters...

    It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

提交回复
热议问题