开发者

Difference between char* and char[]

I know this is a very basic question. I am confused开发者_Go百科 as to why and how are the following different.

char str[] = "Test";
char *str = "Test";


char str[] = "Test";

Is an array of chars, initialized with the contents from "Test", while

char *str = "Test";

is a pointer to the literal (const) string "Test".

The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of "Test", while the pointer simply refers to the contents of the string (which in this case is immutable).


The diference is the STACK memory used.

For example when programming for microcontrollers where very little memory for the stack is allocated, makes a big difference.

char a[] = "string"; // the compiler puts {'s','t','r','i','n','g', 0} onto STACK 

char *a = "string"; // the compiler puts just the pointer onto STACK 
                    // and {'s','t','r','i','n','g',0} in static memory area.


A pointer can be re-pointed to something else:

char foo[] = "foo";
char bar[] = "bar";

char *str = foo;  // str points to 'f'
str = bar;        // Now str points to 'b'
++str;            // Now str points to 'a'

The last example of incrementing the pointer shows that you can easily iterate over the contents of a string, one element at a time.


One is pointer and one is array. They are different type of data.

int main ()
{
   char str1[] = "Test";
   char *str2 = "Test";
   cout << "sizeof array " << sizeof(str1) << endl;
   cout << "sizeof pointer " << sizeof(str2) << endl;
}

output

sizeof array 5
sizeof pointer 4


The first

char str[] = "Test";

is an array of five characters, initialized with the value "Test" plus the null terminator '\0'.

The second

char *str = "Test";

is a pointer to the memory location of the literal string "Test".


Starting from C++11, the second expression is now invalid and must be written:

const char *str = "Test";

The relevant section of the standard is Appendix C section 1.1:

Change: String literals made const

The type of a string literal is changed from “array of char” to “array of const char.” The type of a char16_t string literal is changed from “array of some-integer-type” to “array of const char16_t.” The type of a char32_t string literal is changed from “array of some-integer-type” to “array of const char32_t.” The type of a wide string literal is changed from “array of wchar_t” to “array of const wchar_t.”

Rationale: This avoids calling an inappropriate overloaded function, which might expect to be able to modify its argument.

Effect on original feature: Change to semantics of well-defined feature.


"Test" is an array of five characters (4 letters, plus the null terminator.

char str1[] = "Test"; creates that array of 5 characters, and names it str1. You can modify the contents of that array as much as you like, e.g. str1[0] = 'B';

char *str2 = "Test"; creates that array of 5 characters, doesn't name it, and also creates a pointer named str2. It sets str2 to point at that array of 5 characters. You can follow the pointer to modify the array as much as you like, e.g. str2[0] = 'B'; or *str2 = 'B';. You can even reassign that pointer to point someplace else, e.g. str2 = "other";.

An array is the text in quotes. The pointer merely points at it. You can do a lot of similar things with each, but they are different:

char str_arr[] = "Test";
char *strp = "Test";

// modify
str_arr[0] = 'B'; // ok, str_arr is now "Best"
strp[0] = 'W';    // ok, strp now points at "West"
*strp = 'L';      // ok, strp now points at "Lest"

// point to another string
char another[] = "another string";
str_arr = another;  // compilation error.  you cannot reassign an array
strp = another;     // ok, strp now points at "another string"

// size
std::cout << sizeof(str_arr) << '\n';  // prints 5, because str_arr is five bytes
std::cout << sizeof(strp) << '\n';     // prints 4, because strp is a pointer

for that last part, note that sizeof(strp) is going to vary based on architecture. On a 32-bit machine, it will be 4 bytes, on a 64-bit machine it will be 8 bytes.


Let's take a look at the following ways to declare a string:

char name0 = 'abcd';           // cannot be anything longer than 4 letters (larger causes error)
cout << sizeof(name0) << endl; // using 1 byte to store

char name1[]="abcdefghijklmnopqrstuvwxyz"; // can represent very long strings
cout << sizeof(name1) << endl;             // use large stack memory

char* name2 = "abcdefghijklmnopqrstuvwxyz"; // can represent very long strings
cout << sizeof(name2) << endl;              // but use only 8 bytes

We could see that declaring string using char* variable_name seems the best way! It does the job with minimum stack memory required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜