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

前端 未结 8 1212
太阳男子
太阳男子 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 07:57

    a declares an array of char values -- an array of chars which is terminated.

    p declares a pointer, which refers to an immutable, terminated, C string, whose exact storage location is implementation-defined. Note that this should be const-qualified (e.g. const char *p = "string";).

    If you print it out using std::cout << "a: " << sizeof(a) << "\np: " << sizeof(p) << std::endl;, you will see differences their sizes (note: values may vary by system):

    a: 7
    p: 8
    

    Here what is ? operator? Is it a part of a string or it has some specific meaning?

    char a[] = ?string?
    

    I assume they were once double quotes "string", which potentially were converted to "smart quotes", then could not be represented as such along the way, and were converted to ?.

提交回复
热议问题